Skip to content

TST: Add test for grouby after dropna agg nunique and unique #57711

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 2 commits into from
Mar 5, 2024
Merged
Changes from all 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
20 changes: 20 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2945,3 +2945,23 @@ def test_decimal_na_sort(test_series):
result = gb._grouper.result_index
expected = Index([Decimal(1), None], name="key")
tm.assert_index_equal(result, expected)


def test_groupby_dropna_with_nunique_unique():
# GH#42016
df = [[1, 1, 1, "A"], [1, None, 1, "A"], [1, None, 2, "A"], [1, None, 3, "A"]]
df_dropna = DataFrame(df, columns=["a", "b", "c", "partner"])
result = df_dropna.groupby(["a", "b", "c"], dropna=False).agg(
{"partner": ["nunique", "unique"]}
)

index = MultiIndex.from_tuples(
[(1, 1.0, 1), (1, np.nan, 1), (1, np.nan, 2), (1, np.nan, 3)],
names=["a", "b", "c"],
)
columns = MultiIndex.from_tuples([("partner", "nunique"), ("partner", "unique")])
expected = DataFrame(
[(1, ["A"]), (1, ["A"]), (1, ["A"]), (1, ["A"])], index=index, columns=columns
)

tm.assert_frame_equal(result, expected)