Skip to content
Prev Previous commit
Next Next commit
prevent fast_apply case for _has_complex_internals
  • Loading branch information
jschendel committed Jan 27, 2020
commit 1018ca9383e672fd6f606d0cc7adb4b1d82cc2bf
4 changes: 2 additions & 2 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ def apply(self, f, data: FrameOrSeries, axis: int = 0):
com.get_callable_name(f) not in base.plotting_methods
and isinstance(splitter, FrameSplitter)
and axis == 0
# apply_frame_axis0 doesn't allow MultiIndex
and not isinstance(sdata.index, MultiIndex)
# fast_apply/libreduction doesn't allow non-numpy backed indexes
and not sdata.index._has_complex_internals
):
try:
result_values, mutated = splitter.fast_apply(f, group_keys)
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/groupby/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,3 +811,19 @@ def test_groupby_apply_datetime_result_dtypes():
index=["observation", "color", "mood", "intensity", "score"],
)
tm.assert_series_equal(result, expected)


@pytest.mark.parametrize(
"index",
[
pd.CategoricalIndex(list("abc")),
pd.interval_range(0, 3),
pd.period_range("2020", periods=3, freq="D"),
pd.MultiIndex.from_tuples([("a", 0), ("a", 1), ("b", 0)]),
],
)
def test_apply_index_has_complex_internals(index):
# GH 31248
df = DataFrame({"group": [1, 1, 2], "value": [0, 1, 0]}, index=index)
result = df.groupby("group").apply(lambda x: x)
tm.assert_frame_equal(result, df)