Skip to content

Commit 0e1d56a

Browse files
jbrockmendeljreback
authored andcommitted
Update MultiIndex checks (#29494)
1 parent cd04be2 commit 0e1d56a

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

pandas/_libs/reduction.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,8 @@ def apply_frame_axis0(object frame, object f, object names,
493493
object piece
494494
dict item_cache
495495

496-
if frame.index._has_complex_internals:
497-
raise InvalidApply('Cannot modify frame index internals')
496+
# We have already checked that we don't have a MultiIndex before calling
497+
assert frame.index.nlevels == 1
498498

499499
results = []
500500

@@ -625,7 +625,7 @@ def compute_reduction(arr, f, axis=0, dummy=None, labels=None):
625625

626626
if labels is not None:
627627
# Caller is responsible for ensuring we don't have MultiIndex
628-
assert not labels._has_complex_internals
628+
assert labels.nlevels == 1
629629

630630
# pass as an ndarray/ExtensionArray
631631
labels = labels._values

pandas/core/groupby/ops.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -164,27 +164,26 @@ def apply(self, f, data: FrameOrSeries, axis: int = 0):
164164
com.get_callable_name(f) not in base.plotting_methods
165165
and isinstance(splitter, FrameSplitter)
166166
and axis == 0
167-
# with MultiIndex, apply_frame_axis0 would raise InvalidApply
168-
# TODO: can we make this check prettier?
169-
and not sdata.index._has_complex_internals
167+
# apply_frame_axis0 doesn't allow MultiIndex
168+
and not isinstance(sdata.index, MultiIndex)
170169
):
171170
try:
172171
result_values, mutated = splitter.fast_apply(f, group_keys)
173172

174-
# If the fast apply path could be used we can return here.
175-
# Otherwise we need to fall back to the slow implementation.
176-
if len(result_values) == len(group_keys):
177-
return group_keys, result_values, mutated
178-
179173
except libreduction.InvalidApply as err:
180-
# Cannot fast apply on MultiIndex (_has_complex_internals).
181-
# This Exception is also raised if `f` triggers an exception
174+
# This Exception is raised if `f` triggers an exception
182175
# but it is preferable to raise the exception in Python.
183176
if "Let this error raise above us" not in str(err):
184177
# TODO: can we infer anything about whether this is
185178
# worth-retrying in pure-python?
186179
raise
187180

181+
else:
182+
# If the fast apply path could be used we can return here.
183+
# Otherwise we need to fall back to the slow implementation.
184+
if len(result_values) == len(group_keys):
185+
return group_keys, result_values, mutated
186+
188187
for key, (i, group) in zip(group_keys, splitter):
189188
object.__setattr__(group, "name", key)
190189

@@ -619,7 +618,7 @@ def agg_series(self, obj: Series, func):
619618
# TODO: is the datetime64tz case supposed to go through here?
620619
return self._aggregate_series_pure_python(obj, func)
621620

622-
elif obj.index._has_complex_internals:
621+
elif isinstance(obj.index, MultiIndex):
623622
# MultiIndex; Pre-empt TypeError in _aggregate_series_fast
624623
return self._aggregate_series_pure_python(obj, func)
625624

0 commit comments

Comments
 (0)