From 9ad6d529075915365fa74e84006b561ced82db78 Mon Sep 17 00:00:00 2001 From: KevsterAmp Date: Tue, 6 May 2025 21:09:04 +0800 Subject: [PATCH 1/3] 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 4e2e363885594..6c0128386c0f9 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2814,6 +2814,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, using_cow=using_copy_on_write()) return self._constructor_from_mgr(new_mgr, axes=new_mgr.axes).__finalize__( self, method="round" From 52ffa22f12d878e10e33a1e8b3fbef94bbd08e60 Mon Sep 17 00:00:00 2001 From: KevsterAmp Date: Tue, 6 May 2025 21:19:41 +0800 Subject: [PATCH 2/3] 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 825b23164f5e6433cb0156d7fb56aedb059109b8 Mon Sep 17 00:00:00 2001 From: KevsterAmp Date: Tue, 6 May 2025 21:31:50 +0800 Subject: [PATCH 3/3] 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() -