Skip to content

Commit

Permalink
BUG: as_index=False can return a MultiIndex in groupby.apply (pandas-…
Browse files Browse the repository at this point in the history
  • Loading branch information
undermyumbrella1 authored and pmhatre1 committed May 7, 2024
1 parent 37f02ab commit 512153e
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 19 deletions.
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

0 comments on commit 512153e

Please sign in to comment.