diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 03ad8ed162c95..17d6226d2c60a 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -210,6 +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:`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/core/series.py b/pandas/core/series.py index b40e71f2b5b42..7548b29bb28ea 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2514,7 +2514,14 @@ 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.") + 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..9f13cc5efa0c1 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#62174 + 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()