Skip to content

Commit b961ff5

Browse files
committed
REGR: groupby.value_counts with all NA values
1 parent 02267e5 commit b961ff5

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

pandas/core/groupby/ops.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,9 @@ def _ob_index_and_ids(
881881
sorter = ob_index.argsort()
882882
ob_index = ob_index.take(sorter)
883883
_, index = np.unique(sorter, return_index=True)
884-
ob_ids = np.where(ob_ids == -1, -1, index.take(ob_ids))
884+
na_ids = ob_ids == -1
885+
if not na_ids.all():
886+
ob_ids = np.where(na_ids, -1, index.take(ob_ids))
885887
ob_ids = ensure_platform_int(ob_ids)
886888
return ob_index, ob_ids
887889

pandas/tests/groupby/methods/test_value_counts.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,3 +1219,25 @@ def test_value_counts_sort_categorical(sort, vc_sort, normalize):
12191219
expected = expected.take(taker)
12201220

12211221
tm.assert_series_equal(result, expected)
1222+
1223+
1224+
@pytest.mark.parametrize("groupby_sort", [True, False])
1225+
def test_value_counts_all_na(sort, dropna, groupby_sort):
1226+
# GH#59989
1227+
df = DataFrame({"a": [2, 1, 1], "b": np.nan})
1228+
gb = df.groupby("a", sort=groupby_sort)
1229+
result = gb.value_counts(sort=sort, dropna=dropna)
1230+
1231+
kwargs = {"levels": [[1, 2], [np.nan]], "names": ["a", "b"]}
1232+
if dropna:
1233+
data = []
1234+
index = MultiIndex(codes=[[], []], **kwargs)
1235+
elif not groupby_sort and not sort:
1236+
data = [1, 2]
1237+
index = MultiIndex(codes=[[1, 0], [0, 0]], **kwargs)
1238+
else:
1239+
data = [2, 1]
1240+
index = MultiIndex(codes=[[0, 1], [0, 0]], **kwargs)
1241+
expected = Series(data, index=index, dtype="int64", name="count")
1242+
1243+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)