Skip to content

BUG: SeriesGroupBy.value_counts() raising error on an empty Series #39326

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 5 commits into from
Jan 22, 2021
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ Groupby/resample/rolling
^^^^^^^^^^^^^^^^^^^^^^^^

- Bug in :meth:`SeriesGroupBy.value_counts` where unobserved categories in a grouped categorical series were not tallied (:issue:`38672`)
- Bug in :meth:`SeriesGroupBy.value_counts` where error was raised on an empty series (:issue:`39172`)
- Bug in :meth:`.GroupBy.indices` would contain non-existent indices when null values were present in the groupby keys (:issue:`9304`)
- Fixed bug in :meth:`DataFrameGroupBy.sum` and :meth:`SeriesGroupBy.sum` causing loss of precision through using Kahan summation (:issue:`38778`)
- Fixed bug in :meth:`DataFrameGroupBy.cumsum`, :meth:`SeriesGroupBy.cumsum`, :meth:`DataFrameGroupBy.mean` and :meth:`SeriesGroupBy.mean` causing loss of precision through using Kahan summation (:issue:`38934`)
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,11 +730,16 @@ def apply_series_value_counts():
ids, lab = ids[sorter], lab[sorter]

# group boundaries are where group ids change
idx = np.r_[0, 1 + np.nonzero(ids[1:] != ids[:-1])[0]]
idchanges = 1 + np.nonzero(ids[1:] != ids[:-1])[0]
idx = np.r_[0, idchanges]
if not len(ids):
idx = idchanges

# new values are where sorted labels change
lchanges = llab(lab, slice(1, None)) != llab(lab, slice(None, -1))
inc = np.r_[True, lchanges]
if not len(lchanges):
inc = lchanges
inc[idx] = True # group boundaries are also new values
out = np.diff(np.nonzero(np.r_[inc, True])[0]) # value counts

Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/groupby/test_value_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,27 @@ def test_series_groupby_value_counts_with_grouper():
tm.assert_series_equal(result, expected)


def test_series_groupby_value_counts_empty():
# GH39172
df = DataFrame(columns=["A", "B"])
dfg = df.groupby("A")

result = dfg["B"].value_counts()
expected = Series([], name="B", dtype=result.dtype)
expected.index = MultiIndex.from_arrays([[]] * 2, names=["A", "B"])

tm.assert_series_equal(result, expected)

df = DataFrame(columns=["A", "B", "C"])
dfg = df.groupby(["A", "B"])

result = dfg["C"].value_counts()
expected = Series([], name="C", dtype=result.dtype)
expected.index = MultiIndex.from_arrays([[]] * 3, names=["A", "B", "C"])

tm.assert_series_equal(result, expected)


def test_series_groupby_value_counts_on_categorical():
# GH38672

Expand Down