-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: retain ordered Categorical dtype in SeriesGroupBy aggregations #41147
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -358,9 +358,26 @@ def _cython_agg_general( | |
if numeric_only and not is_numeric: | ||
continue | ||
|
||
result = self.grouper._cython_operation( | ||
"aggregate", obj._values, how, axis=0, min_count=min_count | ||
) | ||
objvals = obj._values | ||
|
||
if isinstance(objvals, Categorical): | ||
if self.grouper.ngroups > 0: | ||
# without special-casing, we would raise, then in fallback | ||
# would eventually call agg_series but without re-casting | ||
# to Categorical | ||
# equiv: res_values, _ = self.grouper.agg_series(obj, alt) | ||
res_values, _ = self.grouper._aggregate_series_pure_python(obj, alt) | ||
else: | ||
# equiv: res_values = self._python_agg_general(alt) | ||
res_values = self._python_apply_general(alt, self._selected_obj) | ||
|
||
result = type(objvals)._from_sequence(res_values, dtype=objvals.dtype) | ||
|
||
else: | ||
result = self.grouper._cython_operation( | ||
"aggregate", obj._values, how, axis=0, min_count=min_count | ||
) | ||
|
||
assert result.ndim == 1 | ||
key = base.OutputKey(label=name, position=idx) | ||
output[key] = result | ||
|
@@ -1092,13 +1109,7 @@ def cast_agg_result(result: ArrayLike, values: ArrayLike) -> ArrayLike: | |
# see if we can cast the values to the desired dtype | ||
# this may not be the original dtype | ||
|
||
if isinstance(values, Categorical) and isinstance(result, np.ndarray): | ||
# If the Categorical op didn't raise, it is dtype-preserving | ||
# We get here with how="first", "last", "min", "max" | ||
result = type(values)._from_sequence(result.ravel(), dtype=values.dtype) | ||
# Note this will have result.dtype == dtype from above | ||
|
||
elif ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in conjunction with #41086, cast_agg_result becomes a 1-liner and can be cleanly tacked onto the end of |
||
if ( | ||
not using_array_manager | ||
and isinstance(result.dtype, np.dtype) | ||
and result.ndim == 1 | ||
|
@@ -1140,9 +1151,14 @@ def py_fallback(values: ArrayLike) -> ArrayLike: | |
# Categoricals. This will done by later self._reindex_output() | ||
# Doing it here creates an error. See GH#34951 | ||
sgb = get_groupby(obj, self.grouper, observed=True) | ||
|
||
# Note: bc obj is always a Series here, we can ignore axis and pass | ||
# `alt` directly instead of `lambda x: alt(x, axis=self.axis)` | ||
res_ser = sgb.aggregate(alt) # this will go through sgb._python_agg_general | ||
# use _agg_general bc it will go through _cython_agg_general | ||
# which will correctly cast Categoricals. | ||
res_ser = sgb._agg_general( | ||
numeric_only=False, min_count=min_count, alias=how, npfunc=alt | ||
) | ||
|
||
# unwrap Series to get array | ||
res_values = res_ser._mgr.arrays[0] | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to try to separate this out further upstream in the future, so that _cython_agg_general isn't calling pure-python methods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally we'll get this case handled within WrappedCythonOp._ea_wrap_cython_operation before too long, but im still troubleshooting that