Skip to content

Commit fc08415

Browse files
authored
BUG: SeriesGroupBy.value_counts() raising error on an empty Series (#39326)
1 parent 7e531e3 commit fc08415

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ Groupby/resample/rolling
345345
^^^^^^^^^^^^^^^^^^^^^^^^
346346

347347
- Bug in :meth:`SeriesGroupBy.value_counts` where unobserved categories in a grouped categorical series were not tallied (:issue:`38672`)
348+
- Bug in :meth:`SeriesGroupBy.value_counts` where error was raised on an empty series (:issue:`39172`)
348349
- Bug in :meth:`.GroupBy.indices` would contain non-existent indices when null values were present in the groupby keys (:issue:`9304`)
349350
- Fixed bug in :meth:`DataFrameGroupBy.sum` and :meth:`SeriesGroupBy.sum` causing loss of precision through using Kahan summation (:issue:`38778`)
350351
- 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`)

pandas/core/groupby/generic.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -730,11 +730,16 @@ def apply_series_value_counts():
730730
ids, lab = ids[sorter], lab[sorter]
731731

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

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

pandas/tests/groupby/test_value_counts.py

+21
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,27 @@ def test_series_groupby_value_counts_with_grouper():
122122
tm.assert_series_equal(result, expected)
123123

124124

125+
def test_series_groupby_value_counts_empty():
126+
# GH39172
127+
df = DataFrame(columns=["A", "B"])
128+
dfg = df.groupby("A")
129+
130+
result = dfg["B"].value_counts()
131+
expected = Series([], name="B", dtype=result.dtype)
132+
expected.index = MultiIndex.from_arrays([[]] * 2, names=["A", "B"])
133+
134+
tm.assert_series_equal(result, expected)
135+
136+
df = DataFrame(columns=["A", "B", "C"])
137+
dfg = df.groupby(["A", "B"])
138+
139+
result = dfg["C"].value_counts()
140+
expected = Series([], name="C", dtype=result.dtype)
141+
expected.index = MultiIndex.from_arrays([[]] * 3, names=["A", "B", "C"])
142+
143+
tm.assert_series_equal(result, expected)
144+
145+
125146
def test_series_groupby_value_counts_on_categorical():
126147
# GH38672
127148

0 commit comments

Comments
 (0)