From 88ea9cb73c72110ad1531acbfeb3f1d8fc924a32 Mon Sep 17 00:00:00 2001 From: KevsterAmp Date: Tue, 6 May 2025 21:09:04 +0800 Subject: [PATCH 1/6] raise TypeError when Series dtype is object on calling Series.round() --- pandas/core/series.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/core/series.py b/pandas/core/series.py index 5ed094349caaa..7d887105d71ae 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2514,6 +2514,8 @@ def round(self, decimals: int = 0, *args, **kwargs) -> Series: dtype: float64 """ nv.validate_round(args, kwargs) + if self._mgr.dtype == "object": + raise TypeError("Expected numeric dtype, got object instead.") new_mgr = self._mgr.round(decimals=decimals) return self._constructor_from_mgr(new_mgr, axes=new_mgr.axes).__finalize__( self, method="round" From 428ba133298c0853b40e102e370a209bc393e586 Mon Sep 17 00:00:00 2001 From: KevsterAmp Date: Tue, 6 May 2025 21:19:41 +0800 Subject: [PATCH 2/6] add test --- pandas/tests/series/methods/test_round.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pandas/tests/series/methods/test_round.py b/pandas/tests/series/methods/test_round.py index c330b7a7dfbbb..7075b6e9fb6be 100644 --- a/pandas/tests/series/methods/test_round.py +++ b/pandas/tests/series/methods/test_round.py @@ -72,3 +72,10 @@ def test_round_ea_boolean(self): tm.assert_series_equal(result, expected) result.iloc[0] = False tm.assert_series_equal(ser, expected) + + def test_round_dtype_object(self): + ser = Series([0.2], dtype="object") + msg = "Expected numeric dtype, got object instead." + with pytest.raises(TypeError, match=msg): + ser.round() + From 20a74d48df0c2be2e710e8e7fa1cbe6c9649e32a Mon Sep 17 00:00:00 2001 From: KevsterAmp Date: Tue, 6 May 2025 21:31:50 +0800 Subject: [PATCH 3/6] precommit --- pandas/tests/series/methods/test_round.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/series/methods/test_round.py b/pandas/tests/series/methods/test_round.py index 7075b6e9fb6be..cb9786a41afb3 100644 --- a/pandas/tests/series/methods/test_round.py +++ b/pandas/tests/series/methods/test_round.py @@ -78,4 +78,3 @@ def test_round_dtype_object(self): msg = "Expected numeric dtype, got object instead." with pytest.raises(TypeError, match=msg): ser.round() - From b98e2e1eb457c04c0fd49c93457b6001b6fc6c92 Mon Sep 17 00:00:00 2001 From: KevsterAmp Date: Mon, 19 May 2025 20:30:33 +0800 Subject: [PATCH 4/6] add github issue reference to test --- pandas/tests/series/methods/test_round.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/series/methods/test_round.py b/pandas/tests/series/methods/test_round.py index cb9786a41afb3..a78f77e990ae1 100644 --- a/pandas/tests/series/methods/test_round.py +++ b/pandas/tests/series/methods/test_round.py @@ -74,6 +74,7 @@ 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." with pytest.raises(TypeError, match=msg): From 9e24d6f61b97dc9d19990dd544758bbbac08585f Mon Sep 17 00:00:00 2001 From: KevsterAmp Date: Mon, 19 May 2025 20:34:22 +0800 Subject: [PATCH 5/6] add bugfix to 2.3 whatsnew --- doc/source/whatsnew/v2.3.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.3.0.rst b/doc/source/whatsnew/v2.3.0.rst index 65bd941800294..1f170f4153fd0 100644 --- a/doc/source/whatsnew/v2.3.0.rst +++ b/doc/source/whatsnew/v2.3.0.rst @@ -120,6 +120,7 @@ Timezones Numeric ^^^^^^^ - 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`) +- Bug in :meth:`Series.round` on object columns no longer raises ``TypeError`` - Conversion From 8c472b961d8bb385d487c930c09db92147a0146a Mon Sep 17 00:00:00 2001 From: Kevin Amparado <109636487+KevsterAmp@users.noreply.github.com> Date: Tue, 20 May 2025 07:46:40 +0800 Subject: [PATCH 6/6] change self._mgr.dtype to self.dtype Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- pandas/core/series.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 7d887105d71ae..2ecb2797a60cc 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2514,7 +2514,7 @@ def round(self, decimals: int = 0, *args, **kwargs) -> Series: dtype: float64 """ nv.validate_round(args, kwargs) - if self._mgr.dtype == "object": + if self.dtype == "object": raise TypeError("Expected numeric dtype, got object instead.") new_mgr = self._mgr.round(decimals=decimals) return self._constructor_from_mgr(new_mgr, axes=new_mgr.axes).__finalize__(