Skip to content
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

BUG: as_index=False can return a MultiIndex in groupby.apply #58369

Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ Groupby/resample/rolling
- Bug in :meth:`.Resampler.interpolate` on a :class:`DataFrame` with non-uniform sampling and/or indices not aligning with the resulting resampled index would result in wrong interpolation (:issue:`21351`)
- Bug in :meth:`DataFrame.ewm` and :meth:`Series.ewm` when passed ``times`` and aggregation functions other than mean (:issue:`51695`)
- Bug in :meth:`DataFrameGroupBy.apply` that was returning a completely empty DataFrame when all return values of ``func`` were ``None`` instead of returning an empty DataFrame with the original columns and dtypes. (:issue:`57775`)
- Bug in :meth:`DataFrameGroupBy.apply` with ``as_index=False`` that was returning :class:`MultiIndex` instead of returning :class:`Index`. (:issue:`58291`)


Reshaping
Expand Down
5 changes: 1 addition & 4 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1202,10 +1202,7 @@ def _concat_objects(
sort=False,
)
else:
# GH5610, returns a MI, with the first level being a
# range index
keys = RangeIndex(len(values))
result = concat(values, axis=0, keys=keys)
result = concat(values, axis=0)

elif not not_indexed_same:
result = concat(values, axis=0)
Expand Down
9 changes: 3 additions & 6 deletions pandas/tests/groupby/methods/test_value_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,10 @@ def test_against_frame_and_seriesgroupby(
else:
name = "proportion" if normalize else "count"
expected = expected.reset_index().rename({0: name}, axis=1)
if groupby == "column":
expected = expected.rename({"level_0": "country"}, axis=1)
expected["country"] = np.where(expected["country"], "US", "FR")
elif groupby == "function":
expected["level_0"] = expected["level_0"] == 1
if groupby in ["array", "function"] and (not as_index and frame):
expected.insert(loc=0, column="level_0", value=result["level_0"])
else:
expected["level_0"] = np.where(expected["level_0"], "US", "FR")
expected.insert(loc=0, column="country", value=result["country"])
tm.assert_frame_equal(result, expected)
else:
# compare against SeriesGroupBy value_counts
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/groupby/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def test_groupby_as_index_apply():

# apply doesn't maintain the original ordering
# changed in GH5610 as the as_index=False returns a MI here
exp_not_as_apply = MultiIndex.from_tuples([(0, 0), (0, 2), (1, 1), (2, 4)])
exp_not_as_apply = Index([0, 2, 1, 4])
tp = [(1, 0), (1, 2), (2, 1), (3, 4)]
exp_as_apply = MultiIndex.from_tuples(tp, names=["user_id", None])

Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/groupby/test_apply_mutate.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ def fn(x):
result = df.groupby(["col1"], as_index=False).apply(fn)
expected = pd.Series(
[1, 2, 0, 4, 5, 0],
index=pd.MultiIndex.from_tuples(
[(0, 0), (0, 1), (0, 2), (1, 3), (1, 4), (1, 5)]
),
index=range(6),
name="col2",
)
tm.assert_series_equal(result, expected)
9 changes: 4 additions & 5 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ def f(x, q=None, axis=0):
expected_seq = df_grouped.quantile([0.4, 0.8])
if not as_index:
# apply treats the op as a transform; .quantile knows it's a reduction
apply_result = apply_result.reset_index()
apply_result["level_0"] = [1, 1, 2, 2]
apply_result.index = range(4)
apply_result.insert(loc=0, column="level_0", value=[1, 1, 2, 2])
apply_result.insert(loc=1, column="level_1", value=[0.4, 0.8, 0.4, 0.8])
tm.assert_frame_equal(apply_result, expected_seq, check_names=False)

agg_result = df_grouped.agg(f, q=80)
Expand Down Expand Up @@ -519,9 +520,7 @@ def test_as_index_select_column():
result = df.groupby("A", as_index=False, group_keys=True)["B"].apply(
lambda x: x.cumsum()
)
expected = Series(
[2, 6, 6], name="B", index=MultiIndex.from_tuples([(0, 0), (0, 1), (1, 2)])
)
expected = Series([2, 6, 6], name="B", index=range(3))
tm.assert_series_equal(result, expected)


Expand Down
Loading