Skip to content

Commit

Permalink
REGR: groupby.value_counts with all NA values (#59999)
Browse files Browse the repository at this point in the history
* REGR: groupby.value_counts with all NA values

* Better implementation
  • Loading branch information
rhshadrach authored Oct 7, 2024
1 parent 02267e5 commit 37c31af
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
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)

0 comments on commit 37c31af

Please sign in to comment.