Skip to content

Commit 2b7b3ef

Browse files
jbrockmendelproost
authored andcommitted
CLN: AttributeError in _wrap_applied_output (pandas-dev#29195)
1 parent df838f9 commit 2b7b3ef

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

pandas/core/base.py

-4
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,6 @@ def _aggregate_multiple_funcs(self, arg, _level, _axis):
571571

572572
except (TypeError, DataError):
573573
pass
574-
except SpecificationError:
575-
raise
576574
else:
577575
results.append(new_res)
578576

@@ -591,8 +589,6 @@ def _aggregate_multiple_funcs(self, arg, _level, _axis):
591589
except ValueError:
592590
# cannot aggregate
593591
continue
594-
except SpecificationError:
595-
raise
596592
else:
597593
results.append(new_res)
598594
keys.append(col)

pandas/core/groupby/generic.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ def _transform_fast(self, func, func_nm):
480480
out = self._try_cast(out, self.obj)
481481
return Series(out, index=self.obj.index, name=self.obj.name)
482482

483-
def filter(self, func, dropna=True, *args, **kwargs): # noqa
483+
def filter(self, func, dropna=True, *args, **kwargs):
484484
"""
485485
Return a copy of a Series excluding elements from groups that
486486
do not satisfy the boolean criterion specified by func.
@@ -1228,7 +1228,7 @@ def first_not_none(values):
12281228
return self._concat_objects(keys, values, not_indexed_same=True)
12291229

12301230
try:
1231-
if self.axis == 0:
1231+
if self.axis == 0 and isinstance(v, ABCSeries):
12321232
# GH6124 if the list of Series have a consistent name,
12331233
# then propagate that name to the result.
12341234
index = v.index.copy()
@@ -1264,15 +1264,24 @@ def first_not_none(values):
12641264
axis=self.axis,
12651265
).unstack()
12661266
result.columns = index
1267-
else:
1267+
elif isinstance(v, ABCSeries):
12681268
stacked_values = np.vstack([np.asarray(v) for v in values])
12691269
result = DataFrame(
12701270
stacked_values.T, index=v.index, columns=key_index
12711271
)
1272+
else:
1273+
# GH#1738: values is list of arrays of unequal lengths
1274+
# fall through to the outer else clause
1275+
# TODO: sure this is right? we used to do this
1276+
# after raising AttributeError above
1277+
return Series(
1278+
values, index=key_index, name=self._selection_name
1279+
)
12721280

1273-
except (ValueError, AttributeError):
1281+
except ValueError:
1282+
# TODO: not reached in tests; is this still needed?
12741283
# GH1738: values is list of arrays of unequal lengths fall
1275-
# through to the outer else caluse
1284+
# through to the outer else clause
12761285
return Series(values, index=key_index, name=self._selection_name)
12771286

12781287
# if we have date/time like in the original, then coerce dates

pandas/core/groupby/ops.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ def apply(self, f, data, axis=0):
199199
f_name not in base.plotting_methods
200200
and hasattr(splitter, "fast_apply")
201201
and axis == 0
202+
# with MultiIndex, apply_frame_axis0 would raise InvalidApply
203+
# TODO: can we make this check prettier?
204+
and not splitter._get_sorted_data().index._has_complex_internals
202205
):
203206
try:
204207
result_values, mutated = splitter.fast_apply(f, group_keys)
@@ -208,11 +211,14 @@ def apply(self, f, data, axis=0):
208211
if len(result_values) == len(group_keys):
209212
return group_keys, result_values, mutated
210213

211-
except libreduction.InvalidApply:
214+
except libreduction.InvalidApply as err:
212215
# Cannot fast apply on MultiIndex (_has_complex_internals).
213216
# This Exception is also raised if `f` triggers an exception
214217
# but it is preferable to raise the exception in Python.
215-
pass
218+
if "Let this error raise above us" not in str(err):
219+
# TODO: can we infer anything about whether this is
220+
# worth-retrying in pure-python?
221+
raise
216222
except TypeError as err:
217223
if "Cannot convert" in str(err):
218224
# via apply_frame_axis0 if we pass a non-ndarray

0 commit comments

Comments
 (0)