-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Groupby not keeping string dtype for empty objects #55619
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
from pandas.errors import AbstractMethodError | ||
from pandas.util._decorators import cache_readonly | ||
|
||
from pandas.core.dtypes.base import ExtensionDtype | ||
from pandas.core.dtypes.cast import ( | ||
maybe_cast_pointwise_result, | ||
maybe_downcast_to_dtype, | ||
|
@@ -837,10 +838,8 @@ def agg_series( | |
------- | ||
np.ndarray or ExtensionArray | ||
""" | ||
# test_groupby_empty_with_category gets here with self.ngroups == 0 | ||
# and len(obj) > 0 | ||
|
||
if len(obj) > 0 and not isinstance(obj._values, np.ndarray): | ||
if not isinstance(obj._values, np.ndarray): | ||
# we can preserve a little bit more aggressively with EA dtype | ||
# because maybe_cast_pointwise_result will do a try/except | ||
# with _from_sequence. NB we are assuming here that _from_sequence | ||
|
@@ -849,11 +848,18 @@ def agg_series( | |
|
||
result = self._aggregate_series_pure_python(obj, func) | ||
|
||
npvalues = lib.maybe_convert_objects(result, try_float=False) | ||
if preserve_dtype: | ||
out = maybe_cast_pointwise_result(npvalues, obj.dtype, numeric_only=True) | ||
if len(obj) == 0 and len(result) == 0 and isinstance(obj.dtype, ExtensionDtype): | ||
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. What happens on an empty list of categoricals with 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. I didn't track his down specifically, but the test that was mentioned in the comment is not passing by here anymore 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. Running on this PR as-is:
If you remove the condition 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. The new string dtype uses nan as missing value representation to keep numpy semantics, so it will work if you feed it an array with all NaN 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. Ah - thanks 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. I think this still needs fixed (but okay not for 2.1.2) |
||
cls = obj.dtype.construct_array_type() | ||
out = cls._from_sequence(result) | ||
|
||
else: | ||
out = npvalues | ||
npvalues = lib.maybe_convert_objects(result, try_float=False) | ||
if preserve_dtype: | ||
out = maybe_cast_pointwise_result( | ||
npvalues, obj.dtype, numeric_only=True | ||
) | ||
else: | ||
out = npvalues | ||
return out | ||
|
||
@final | ||
|
Uh oh!
There was an error while loading. Please reload this page.