-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
TST: added test for GH28597 #33588
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(): | ||
# 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
maybe one of the issues should be closed as a duplicate? |
||
|
||
tm.assert_frame_equal(result, expected) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem, will do