Skip to content

Commit b0af597

Browse files
committed
rounding boolean shouldnt error
1 parent 398e64b commit b0af597

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

pandas/core/arrays/base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,10 +2282,10 @@ def round(self, decimals: int = 0, *args, **kwargs) -> Self:
22822282
# Implementer note: This is a non-optimized default implementation.
22832283
# Implementers are encouraged to override this method to avoid
22842284
# elementwise rounding.
2285-
if not self.dtype._is_numeric or self.dtype._is_boolean:
2286-
raise TypeError(
2287-
f"Cannot round {type(self)} dtype as it is non-numeric or boolean"
2288-
)
2285+
if self.dtype._is_boolean:
2286+
return self
2287+
if not self.dtype._is_numeric:
2288+
raise TypeError(f"Cannot round {type(self)} dtype as it is non-numeric")
22892289
return self._from_sequence(
22902290
[
22912291
round(element) if not element_isna else element

pandas/tests/extension/base/methods.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,9 +720,13 @@ def test_equals_same_data_different_object(self, data):
720720
assert pd.Series(data).equals(pd.Series(data))
721721

722722
def test_round(self, data):
723-
if not data.dtype._is_numeric or data.dtype._is_boolean:
723+
if not data.dtype._is_numeric:
724724
with pytest.raises(TypeError):
725-
pd.Series(data).round()
725+
data.round()
726+
elif data.dtype._is_boolean:
727+
result = pd.Series(data).round()
728+
expected = pd.Series(data)
729+
tm.assert_series_equal(result, expected)
726730
else:
727731
result = pd.Series(data).round()
728732
expected = pd.Series(

0 commit comments

Comments
 (0)