Skip to content

REGR: groupby.value_counts with all NA values #59999

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
Oct 7, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ def _ob_index_and_ids(
names=names,
verify_integrity=False,
)
if not consistent_sorting:
if not consistent_sorting and len(ob_index) > 0:
# Sort by the levels where the corresponding sort argument is True
n_levels = len(sorts)
drop_levels = [
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/groupby/methods/test_value_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -1219,3 +1219,25 @@ def test_value_counts_sort_categorical(sort, vc_sort, normalize):
expected = expected.take(taker)

tm.assert_series_equal(result, expected)


@pytest.mark.parametrize("groupby_sort", [True, False])
def test_value_counts_all_na(sort, dropna, groupby_sort):
# GH#59989
df = DataFrame({"a": [2, 1, 1], "b": np.nan})
gb = df.groupby("a", sort=groupby_sort)
result = gb.value_counts(sort=sort, dropna=dropna)

kwargs = {"levels": [[1, 2], [np.nan]], "names": ["a", "b"]}
if dropna:
data = []
index = MultiIndex(codes=[[], []], **kwargs)
elif not groupby_sort and not sort:
data = [1, 2]
index = MultiIndex(codes=[[1, 0], [0, 0]], **kwargs)
else:
data = [2, 1]
index = MultiIndex(codes=[[0, 1], [0, 0]], **kwargs)
expected = Series(data, index=index, dtype="int64", name="count")

tm.assert_series_equal(result, expected)