Skip to content

Preserving boolean dtype in Series.any/all function with level keyword #33449 #33543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9974,11 +9974,15 @@ def _agg_by_level(self, name, axis=0, level=0, skipna=True, **kwargs):
raise ValueError("Must specify 'axis' when aggregating by level.")
grouped = self.groupby(level=level, axis=axis, sort=False)
if hasattr(grouped, name) and skipna:
return getattr(grouped, name)(**kwargs)
axis = self._get_axis_number(axis)
method = getattr(type(self), name)
applyf = lambda x: method(x, axis=axis, skipna=skipna, **kwargs)
return grouped.aggregate(applyf)
result = getattr(grouped, name)(**kwargs)
else:
axis = self._get_axis_number(axis)
method = getattr(type(self), name)
applyf = lambda x: method(x, axis=axis, skipna=skipna, **kwargs)
result = grouped.aggregate(applyf)
if isinstance(self, ABCSeries) and self.dtype.name == "boolean":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you instead trace exactly where this is not getting converted propertly; likely its in EA method itself. We can to handle much lower down rather than a specific case here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In pandas/core/groupby/groupby.py file, there is a _get_cythonized_result() function which has takes a pre_processing lambda where the return type of result is decided.
We can have an if/else statement there which checks the dtype of input and decides the dtype of the result.

return result.astype("boolean")
return result

@classmethod
def _add_numeric_operations(cls):
Expand Down
9 changes: 7 additions & 2 deletions pandas/tests/reductions/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,8 +912,13 @@ def test_all_any_boolean(self):
index=[0, 0, 1, 1, 2, 2],
dtype="boolean",
)
tm.assert_series_equal(s.all(level=0), Series([False, True, False]))
tm.assert_series_equal(s.any(level=0), Series([False, True, True]))
result = s.all(level=0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you pull L910 out to a new test. Then parameterize on bool and boolean.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay

expected = Series([False, True, False], dtype="boolean")
tm.assert_series_equal(result, expected)

result = s.any(level=0)
expected = Series([False, True, True], dtype="boolean")
tm.assert_series_equal(result, expected)

def test_timedelta64_analytics(self):

Expand Down