Skip to content

Commit 6a2da7a

Browse files
BUG: round on object columns no longer raises a TypeError (#61399)
* raise TypeError when Series dtype is object on calling Series.round() * add test * precommit * add github issue reference to test * add bugfix to 2.3 whatsnew * change self._mgr.dtype to self.dtype Co-authored-by: Matthew Roeschke <[email protected]> --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent fe60f43 commit 6a2da7a

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

doc/source/whatsnew/v2.3.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ Timezones
120120
Numeric
121121
^^^^^^^
122122
- Enabled :class:`Series.mode` and :class:`DataFrame.mode` with ``dropna=False`` to sort the result for all dtypes in the presence of NA values; previously only certain dtypes would sort (:issue:`60702`)
123+
- Bug in :meth:`Series.round` on object columns no longer raises ``TypeError``
123124
-
124125

125126
Conversion

pandas/core/series.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,6 +2514,8 @@ def round(self, decimals: int = 0, *args, **kwargs) -> Series:
25142514
dtype: float64
25152515
"""
25162516
nv.validate_round(args, kwargs)
2517+
if self.dtype == "object":
2518+
raise TypeError("Expected numeric dtype, got object instead.")
25172519
new_mgr = self._mgr.round(decimals=decimals)
25182520
return self._constructor_from_mgr(new_mgr, axes=new_mgr.axes).__finalize__(
25192521
self, method="round"

pandas/tests/series/methods/test_round.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,10 @@ def test_round_ea_boolean(self):
7272
tm.assert_series_equal(result, expected)
7373
result.iloc[0] = False
7474
tm.assert_series_equal(ser, expected)
75+
76+
def test_round_dtype_object(self):
77+
# GH#61206
78+
ser = Series([0.2], dtype="object")
79+
msg = "Expected numeric dtype, got object instead."
80+
with pytest.raises(TypeError, match=msg):
81+
ser.round()

0 commit comments

Comments
 (0)