Skip to content

Commit 2690abe

Browse files
jbrockmendelproost
authored andcommitted
REF: Pre-empt TypeError (pandas-dev#29261)
1 parent 25f2772 commit 2690abe

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

pandas/core/groupby/ops.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,20 @@ def apply(self, f, data, axis=0):
140140
group_keys = self._get_group_keys()
141141
result_values = None
142142

143-
# oh boy
144-
f_name = com.get_callable_name(f)
145-
if (
146-
f_name not in base.plotting_methods
143+
sdata = splitter._get_sorted_data()
144+
if sdata.ndim == 2 and np.any(sdata.dtypes.apply(is_extension_array_dtype)):
145+
# calling splitter.fast_apply will raise TypeError via apply_frame_axis0
146+
# if we pass EA instead of ndarray
147+
# TODO: can we have a workaround for EAs backed by ndarray?
148+
pass
149+
150+
elif (
151+
com.get_callable_name(f) not in base.plotting_methods
147152
and hasattr(splitter, "fast_apply")
148153
and axis == 0
149154
# with MultiIndex, apply_frame_axis0 would raise InvalidApply
150155
# TODO: can we make this check prettier?
151-
and not splitter._get_sorted_data().index._has_complex_internals
156+
and not sdata.index._has_complex_internals
152157
):
153158
try:
154159
result_values, mutated = splitter.fast_apply(f, group_keys)
@@ -166,12 +171,6 @@ def apply(self, f, data, axis=0):
166171
# TODO: can we infer anything about whether this is
167172
# worth-retrying in pure-python?
168173
raise
169-
except TypeError as err:
170-
if "Cannot convert" in str(err):
171-
# via apply_frame_axis0 if we pass a non-ndarray
172-
pass
173-
else:
174-
raise
175174

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

0 commit comments

Comments
 (0)