Skip to content

REF: Pre-empt TypeError in groupby #29261

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 2 commits into from
Nov 2, 2019
Merged
Changes from 1 commit
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
21 changes: 10 additions & 11 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,20 @@ def apply(self, f, data, axis=0):
group_keys = self._get_group_keys()
result_values = None

# oh boy
f_name = com.get_callable_name(f)
if (
f_name not in base.plotting_methods
sdata = splitter._get_sorted_data()
if sdata.ndim == 2 and np.any(sdata.dtypes.apply(is_extension_array_dtype)):
Copy link
Contributor

Choose a reason for hiding this comment

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

I would move this to the Splittler class something like

@cache_only
def has_extension_array(self):
     return self.data.dtypes.apply(...)

and can handle this on Series & Frame splitter

Copy link
Member Author

Choose a reason for hiding this comment

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

yah, this function is definitely getting refactored in a later pass. id like to get the exceptions sorted out before trying to decide about the refactor

# calling splitter.fast_apply will raise TypeError via apply_frame_axis0
# if we pass EA instead of ndarray
# TODO: can we have a workaround for EAs backed by ndarray?
pass

elif (
com.get_callable_name(f) not in base.plotting_methods
and hasattr(splitter, "fast_apply")
and axis == 0
# with MultiIndex, apply_frame_axis0 would raise InvalidApply
# TODO: can we make this check prettier?
and not splitter._get_sorted_data().index._has_complex_internals
and not sdata.index._has_complex_internals
):
try:
result_values, mutated = splitter.fast_apply(f, group_keys)
Expand All @@ -219,12 +224,6 @@ def apply(self, f, data, axis=0):
# TODO: can we infer anything about whether this is
# worth-retrying in pure-python?
raise
except TypeError as err:
if "Cannot convert" in str(err):
# via apply_frame_axis0 if we pass a non-ndarray
pass
else:
raise

for key, (i, group) in zip(group_keys, splitter):
object.__setattr__(group, "name", key)
Expand Down