Skip to content

added test to indexing on groupby, #32464 #44046

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

Closed
wants to merge 16 commits into from
Closed
Changes from 6 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
12 changes: 12 additions & 0 deletions pandas/tests/groupby/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -1155,3 +1155,15 @@ def test_groupby_sum_below_mincount_nullable_integer():
result = grouped.sum(min_count=2)
expected = DataFrame({"b": [pd.NA] * 3, "c": [pd.NA] * 3}, dtype="Int64", index=idx)
tm.assert_frame_equal(result, expected)


def test_if_is_multiindex():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rename to something like test_empty_multiindex

# GH 32464
# Test if index after groupby with more then one column is always MultiIndex
a = DataFrame({"a": [], "b": [], "c": []})

agg_1 = a.groupby(["a", "b"]).sum().iloc[:, :0]
agg_2 = a.groupby(["a", "b", "c"]).sum().droplevel("c")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay could you change the assertion back to testing the MultiIndex result if the new assertion can't test the whole frame?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Like this?

# GH 32464
# Test if index after groupby with more then one column is always MultiIndex
a = DataFrame({"a": [], "b": [], "c": []})

agg_1 = a.groupby(["a", "b"]).sum()
agg_2 = a.groupby(["a", "b", "c"]).sum()

# Tests if group by with all columns has a MultiIndex
assert isinstance(agg_2.index, pd.core.indexes.multi.MultiIndex)

result = agg_1.iloc[:, :0]
expected = agg_2.droplevel("c")

# Tests if both agreggations have multiindex
tm.assert_frame_equal(result, expected)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use tm.assert_index_equal


# Tests if both agreggations have multiindex
tm.assert_frame_equal(agg_1, agg_2)