Skip to content

Commit 292e4fd

Browse files
author
Kei
committed
Update tests to return single index
1 parent 70748fd commit 292e4fd

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

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 "level_0" in result:
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_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)