Skip to content

BUG: groupby with CategoricalIndex doesn't include unobserved categories #49373

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 13 commits into from
Nov 7, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Test fixup
  • Loading branch information
rhshadrach committed Oct 28, 2022
commit eb400336dda2f70b706f4bae0a4b707d8acb9e9b
10 changes: 8 additions & 2 deletions pandas/tests/groupby/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,14 +871,20 @@ def test_apply_multi_level_name(category):
b = [1, 2] * 5
if category:
b = pd.Categorical(b, categories=[1, 2, 3])
expected_index = pd.CategoricalIndex([1, 2], categories=[1, 2, 3], name="B")
expected_index = pd.CategoricalIndex([1, 2, 3], categories=[1, 2, 3], name="B")
# GH#40669 - summing an empty frame gives float dtype
expected_values = [20.0, 25.0, 0.0]
else:
expected_index = Index([1, 2], name="B")
expected_values = [20, 25]
expected = DataFrame(
{"C": expected_values, "D": expected_values}, index=expected_index
)

df = DataFrame(
{"A": np.arange(10), "B": b, "C": list(range(10)), "D": list(range(10))}
).set_index(["A", "B"])
result = df.groupby("B").apply(lambda x: x.sum())
expected = DataFrame({"C": [20, 25], "D": [20, 25]}, index=expected_index)
tm.assert_frame_equal(result, expected)
assert df.index.names == ["A", "B"]

Expand Down