From 5f0bd7dfbbc5aab5e53f255459954d17bc11ac0d Mon Sep 17 00:00:00 2001 From: sharkipelago Date: Fri, 22 Aug 2025 13:43:13 -0400 Subject: [PATCH 1/3] changed series.round() object dtype behavior to pointwise --- doc/source/whatsnew/v3.0.0.rst | 1 + pandas/core/series.py | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index b94d82f3c9783..50a9955195154 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -192,6 +192,7 @@ Other enhancements - :meth:`Series.map` can now accept kwargs to pass on to func (:issue:`59814`) - :meth:`Series.map` now accepts an ``engine`` parameter to allow execution with a third-party execution engine (:issue:`61125`) - :meth:`Series.rank` and :meth:`DataFrame.rank` with numpy-nullable dtypes preserve ``NA`` values and return ``UInt64`` dtype where appropriate instead of casting ``NA`` to ``NaN`` with ``float64`` dtype (:issue:`62043`) +- :meth:`Series.round` now operates pointwise on columns of object dtype (:issue:`61682`) - :meth:`Series.str.get_dummies` now accepts a ``dtype`` parameter to specify the dtype of the resulting DataFrame (:issue:`47872`) - :meth:`pandas.concat` will raise a ``ValueError`` when ``ignore_index=True`` and ``keys`` is not ``None`` (:issue:`59274`) - :py:class:`frozenset` elements in pandas objects are now natively printed (:issue:`60690`) diff --git a/pandas/core/series.py b/pandas/core/series.py index 00cff09801f1a..4e0ebb7c6eb35 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2517,7 +2517,11 @@ def round(self, decimals: int = 0, *args, **kwargs) -> Series: """ nv.validate_round(args, kwargs) if self.dtype == "object": - raise TypeError("Expected numeric dtype, got object instead.") + round_func = functools.partial(round, ndigits=decimals) + new_values = self._map_values(round_func) + return self._constructor( + new_values, index=self.index, copy=False + ).__finalize__(self, method="map") new_mgr = self._mgr.round(decimals=decimals) return self._constructor_from_mgr(new_mgr, axes=new_mgr.axes).__finalize__( self, method="round" From a9196468865b141304eaa27fe585832a0249acc8 Mon Sep 17 00:00:00 2001 From: sharkipelago Date: Fri, 22 Aug 2025 18:46:36 -0400 Subject: [PATCH 2/3] updated tests --- doc/source/whatsnew/v3.0.0.rst | 2 +- pandas/core/series.py | 13 ++++++++----- pandas/tests/series/methods/test_round.py | 11 +++++++---- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 50a9955195154..8d6c384712ca8 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -192,7 +192,7 @@ Other enhancements - :meth:`Series.map` can now accept kwargs to pass on to func (:issue:`59814`) - :meth:`Series.map` now accepts an ``engine`` parameter to allow execution with a third-party execution engine (:issue:`61125`) - :meth:`Series.rank` and :meth:`DataFrame.rank` with numpy-nullable dtypes preserve ``NA`` values and return ``UInt64`` dtype where appropriate instead of casting ``NA`` to ``NaN`` with ``float64`` dtype (:issue:`62043`) -- :meth:`Series.round` now operates pointwise on columns of object dtype (:issue:`61682`) +- :meth:`Series.round` now operates pointwise on columns of object dtype (:issue:`62173`) - :meth:`Series.str.get_dummies` now accepts a ``dtype`` parameter to specify the dtype of the resulting DataFrame (:issue:`47872`) - :meth:`pandas.concat` will raise a ``ValueError`` when ``ignore_index=True`` and ``keys`` is not ``None`` (:issue:`59274`) - :py:class:`frozenset` elements in pandas objects are now natively printed (:issue:`60690`) diff --git a/pandas/core/series.py b/pandas/core/series.py index 4e0ebb7c6eb35..6222fa036dddf 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2517,11 +2517,14 @@ def round(self, decimals: int = 0, *args, **kwargs) -> Series: """ nv.validate_round(args, kwargs) if self.dtype == "object": - round_func = functools.partial(round, ndigits=decimals) - new_values = self._map_values(round_func) - return self._constructor( - new_values, index=self.index, copy=False - ).__finalize__(self, method="map") + try: + round_func = functools.partial(round, ndigits=decimals) + new_values = self._map_values(round_func) + return self._constructor( + new_values, index=self.index, copy=False + ).__finalize__(self, method="map") + except TypeError as e: + raise TypeError("Expected numeric entries for dtype object.") from e new_mgr = self._mgr.round(decimals=decimals) return self._constructor_from_mgr(new_mgr, axes=new_mgr.axes).__finalize__( self, method="round" diff --git a/pandas/tests/series/methods/test_round.py b/pandas/tests/series/methods/test_round.py index a78f77e990ae1..d8828200dcd6f 100644 --- a/pandas/tests/series/methods/test_round.py +++ b/pandas/tests/series/methods/test_round.py @@ -74,8 +74,11 @@ def test_round_ea_boolean(self): tm.assert_series_equal(ser, expected) def test_round_dtype_object(self): - # GH#61206 - ser = Series([0.2], dtype="object") - msg = "Expected numeric dtype, got object instead." + # GH#61206, GH#62173 + ser = Series([0.232], dtype="object") + expected = Series([0.2]) + tm.assert_series_equal(ser.round(1), expected) + ser2 = Series(["bar"], dtype="object") + msg = "Expected numeric entries for dtype object." with pytest.raises(TypeError, match=msg): - ser.round() + ser2.round() From fd1f6b69ddaca596f8724c4bee28edb8ef3ced44 Mon Sep 17 00:00:00 2001 From: sharkipelago Date: Fri, 22 Aug 2025 18:58:44 -0400 Subject: [PATCH 3/3] corrected PR number --- doc/source/whatsnew/v3.0.0.rst | 2 +- pandas/tests/series/methods/test_round.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index d414e234ec59d..17d6226d2c60a 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -210,7 +210,7 @@ Other enhancements - :meth:`Series.map` can now accept kwargs to pass on to func (:issue:`59814`) - :meth:`Series.map` now accepts an ``engine`` parameter to allow execution with a third-party execution engine (:issue:`61125`) - :meth:`Series.rank` and :meth:`DataFrame.rank` with numpy-nullable dtypes preserve ``NA`` values and return ``UInt64`` dtype where appropriate instead of casting ``NA`` to ``NaN`` with ``float64`` dtype (:issue:`62043`) -- :meth:`Series.round` now operates pointwise on columns of object dtype (:issue:`62173`) +- :meth:`Series.round` now operates pointwise on columns of object dtype (:issue:`62174`) - :meth:`Series.str.get_dummies` now accepts a ``dtype`` parameter to specify the dtype of the resulting DataFrame (:issue:`47872`) - :meth:`pandas.concat` will raise a ``ValueError`` when ``ignore_index=True`` and ``keys`` is not ``None`` (:issue:`59274`) - :py:class:`frozenset` elements in pandas objects are now natively printed (:issue:`60690`) diff --git a/pandas/tests/series/methods/test_round.py b/pandas/tests/series/methods/test_round.py index d8828200dcd6f..9f13cc5efa0c1 100644 --- a/pandas/tests/series/methods/test_round.py +++ b/pandas/tests/series/methods/test_round.py @@ -74,7 +74,7 @@ def test_round_ea_boolean(self): tm.assert_series_equal(ser, expected) def test_round_dtype_object(self): - # GH#61206, GH#62173 + # GH#61206, GH#62174 ser = Series([0.232], dtype="object") expected = Series([0.2]) tm.assert_series_equal(ser.round(1), expected)