Skip to content

Commit 512153e

Browse files
undermyumbrella1pmhatre1
authored andcommitted
BUG: as_index=False can return a MultiIndex in groupby.apply (pandas-dev#58369)
1 parent 37f02ab commit 512153e

File tree

5 files changed

+10
-19
lines changed

5 files changed

+10
-19
lines changed

pandas/core/groupby/groupby.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1202,10 +1202,7 @@ def _concat_objects(
12021202
sort=False,
12031203
)
12041204
else:
1205-
# GH5610, returns a MI, with the first level being a
1206-
# range index
1207-
keys = RangeIndex(len(values))
1208-
result = concat(values, axis=0, keys=keys)
1205+
result = concat(values, axis=0)
12091206

12101207
elif not not_indexed_same:
12111208
result = concat(values, axis=0)

pandas/tests/groupby/methods/test_value_counts.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -329,13 +329,10 @@ def test_against_frame_and_seriesgroupby(
329329
else:
330330
name = "proportion" if normalize else "count"
331331
expected = expected.reset_index().rename({0: name}, axis=1)
332-
if groupby == "column":
333-
expected = expected.rename({"level_0": "country"}, axis=1)
334-
expected["country"] = np.where(expected["country"], "US", "FR")
335-
elif groupby == "function":
336-
expected["level_0"] = expected["level_0"] == 1
332+
if groupby in ["array", "function"] and (not as_index and frame):
333+
expected.insert(loc=0, column="level_0", value=result["level_0"])
337334
else:
338-
expected["level_0"] = np.where(expected["level_0"], "US", "FR")
335+
expected.insert(loc=0, column="country", value=result["country"])
339336
tm.assert_frame_equal(result, expected)
340337
else:
341338
# compare against SeriesGroupBy value_counts

pandas/tests/groupby/test_apply.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def test_groupby_as_index_apply():
315315

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

pandas/tests/groupby/test_apply_mutate.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ def fn(x):
9090
result = df.groupby(["col1"], as_index=False).apply(fn)
9191
expected = pd.Series(
9292
[1, 2, 0, 4, 5, 0],
93-
index=pd.MultiIndex.from_tuples(
94-
[(0, 0), (0, 1), (0, 2), (1, 3), (1, 4), (1, 5)]
95-
),
93+
index=range(6),
9694
name="col2",
9795
)
9896
tm.assert_series_equal(result, expected)

pandas/tests/groupby/test_groupby.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,9 @@ def f(x, q=None, axis=0):
113113
expected_seq = df_grouped.quantile([0.4, 0.8])
114114
if not as_index:
115115
# apply treats the op as a transform; .quantile knows it's a reduction
116-
apply_result = apply_result.reset_index()
117-
apply_result["level_0"] = [1, 1, 2, 2]
116+
apply_result.index = range(4)
117+
apply_result.insert(loc=0, column="level_0", value=[1, 1, 2, 2])
118+
apply_result.insert(loc=1, column="level_1", value=[0.4, 0.8, 0.4, 0.8])
118119
tm.assert_frame_equal(apply_result, expected_seq, check_names=False)
119120

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

527526

0 commit comments

Comments
 (0)