Skip to content

TST: added test for GH28597 #33588

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

Merged
merged 4 commits into from
Apr 17, 2020
Merged
Changes from 2 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
45 changes: 45 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2012,3 +2012,48 @@ def test_groups_repr_truncates(max_seq_items, expected):

result = df.groupby(np.array(df.a)).groups.__repr__()
assert result == expected


def test_sorted_missing_category_values():
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 move this test to pandas/tests/groupby/test_categorical.py

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No problem, will do

# GH 28597
df = pd.DataFrame(
{
"foo": [
"small",
"large",
"large",
"large",
"medium",
"large",
"large",
"medium",
],
"bar": ["C", "A", "A", "C", "A", "C", "A", "C"],
}
)
df["foo"] = (
df["foo"]
.astype("category")
.cat.set_categories(["tiny", "small", "medium", "large"], ordered=True)
)

expected = pd.DataFrame(
{
"tiny": {"A": 0, "C": 0},
"small": {"A": 0, "C": 1},
"medium": {"A": 1, "C": 1},
"large": {"A": 3, "C": 2},
}
)
expected = expected.rename_axis("bar", axis="index")
expected.columns = pd.CategoricalIndex(
["tiny", "small", "medium", "large"],
categories=["tiny", "small", "medium", "large"],
ordered=True,
name="foo",
dtype="category",
)

result = df.groupby(["bar", "foo"]).size().unstack()
Copy link
Member

Choose a reason for hiding this comment

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

I think the change in behaviour that needs to be tested is the size method and the unstack call is not relevant in the issue OP.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are two separate issues that have been fixed, and I think that this tests them both. One is that missing categorical values in groupby would cause the categories to become unsorted #28597 . That is the one that I have mentioned in the commit message.

The other is that previously, empty groups would be missing from the output even when grouping with categories #23865, which I think this test also catches as a side effect.

Copy link
Member

Choose a reason for hiding this comment

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

IIUC it's the size call that is the root cause of the issues and this issue was resolved in #29690

which I think this test also catches as a side effect

maybe one of the issues should be closed as a duplicate?


tm.assert_frame_equal(result, expected)