From 72307254ed17d10420406122484d73d9e55b29d8 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Sun, 7 Jan 2024 19:15:52 +0100 Subject: [PATCH 1/2] BUG: Series.round raising for nullable bool dtype --- doc/source/whatsnew/v2.2.0.rst | 1 + pandas/core/arrays/masked.py | 2 ++ pandas/core/series.py | 9 +++++---- pandas/tests/series/methods/test_round.py | 9 +++++++++ 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v2.2.0.rst b/doc/source/whatsnew/v2.2.0.rst index 0b04a1d313a6d..8b2ae8dcdfc1e 100644 --- a/doc/source/whatsnew/v2.2.0.rst +++ b/doc/source/whatsnew/v2.2.0.rst @@ -789,6 +789,7 @@ Numeric - Bug in :meth:`Series.__floordiv__` and :meth:`Series.__truediv__` for :class:`ArrowDtype` with integral dtypes raising for large divisors (:issue:`56706`) - Bug in :meth:`Series.__floordiv__` for :class:`ArrowDtype` with integral dtypes raising for large values (:issue:`56645`) - Bug in :meth:`Series.pow` not filling missing values correctly (:issue:`55512`) +- Bug in :meth:`Series.round` raising for nullable boolean dtype (:issue:`55936`) Conversion ^^^^^^^^^^ diff --git a/pandas/core/arrays/masked.py b/pandas/core/arrays/masked.py index 9ce19ced2b356..a3ff616cb8f98 100644 --- a/pandas/core/arrays/masked.py +++ b/pandas/core/arrays/masked.py @@ -404,6 +404,8 @@ def round(self, decimals: int = 0, *args, **kwargs): DataFrame.round : Round values of a DataFrame. Series.round : Round values of a Series. """ + if self.dtype.kind == "b": + return self nv.validate_round(args, kwargs) values = np.round(self._data, decimals=decimals, **kwargs) diff --git a/pandas/core/series.py b/pandas/core/series.py index 90073e21cfd66..3d16a55c8cac4 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2789,13 +2789,14 @@ def round(self, decimals: int = 0, *args, **kwargs) -> Series: dtype: float64 """ nv.validate_round(args, kwargs) - result = self._values.round(decimals) - result = self._constructor(result, index=self.index, copy=False).__finalize__( + new_mgr = self._mgr.round( + decimals=decimals, # type: ignore[arg-type] + using_cow=using_copy_on_write(), + ) + return self._constructor_from_mgr(new_mgr, axes=new_mgr.axes).__finalize__( self, method="round" ) - return result - @overload def quantile( self, q: float = ..., interpolation: QuantileInterpolation = ... diff --git a/pandas/tests/series/methods/test_round.py b/pandas/tests/series/methods/test_round.py index 7f60c94f10e4f..c330b7a7dfbbb 100644 --- a/pandas/tests/series/methods/test_round.py +++ b/pandas/tests/series/methods/test_round.py @@ -63,3 +63,12 @@ def test_round_nat(self, method, freq, unit): round_method = getattr(ser.dt, method) result = round_method(freq) tm.assert_series_equal(result, expected) + + def test_round_ea_boolean(self): + # GH#55936 + ser = Series([True, False], dtype="boolean") + expected = ser.copy() + result = ser.round(2) + tm.assert_series_equal(result, expected) + result.iloc[0] = False + tm.assert_series_equal(ser, expected) From 536a57b112c1ecd4b50f2876e4aad89609ce726c Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Sun, 7 Jan 2024 20:04:14 +0100 Subject: [PATCH 2/2] Fix typing --- pandas/core/series.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 3d16a55c8cac4..8098d990a6a89 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2789,10 +2789,7 @@ def round(self, decimals: int = 0, *args, **kwargs) -> Series: dtype: float64 """ nv.validate_round(args, kwargs) - new_mgr = self._mgr.round( - decimals=decimals, # type: ignore[arg-type] - using_cow=using_copy_on_write(), - ) + 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" )