Skip to content

Commit 86709da

Browse files
Backport PR #61399: BUG: round on object columns no longer raises a TypeError (#61472)
Co-authored-by: Kevin Amparado <109636487+KevsterAmp@users.noreply.github.com>
1 parent 5bbd98b commit 86709da

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
@@ -107,6 +107,7 @@ Timezones
107107
Numeric
108108
^^^^^^^
109109
- 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`)
110+
- Bug in :meth:`Series.round` on object columns no longer raises ``TypeError``
110111
-
111112

112113
Conversion

pandas/core/series.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2814,6 +2814,8 @@ def round(self, decimals: int = 0, *args, **kwargs) -> Series:
28142814
dtype: float64
28152815
"""
28162816
nv.validate_round(args, kwargs)
2817+
if self.dtype == "object":
2818+
raise TypeError("Expected numeric dtype, got object instead.")
28172819
new_mgr = self._mgr.round(decimals=decimals, using_cow=using_copy_on_write())
28182820
return self._constructor_from_mgr(new_mgr, axes=new_mgr.axes).__finalize__(
28192821
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy