Skip to content

BUG: SeriesGroupBy.value_counts() raises on an empty Series #39172

Closed
@FilippoBovo

Description

@FilippoBovo
  • I have searched the [pandas] tag on StackOverflow for similar questions.

  • I have asked my usage related question on StackOverflow.


Question about pandas

Using the value_counts method after grouping an empty DataFrame and selecting a column raises an error.

Example:

>>> pd.DataFrame(columns=["A", "B"]).groupby("A")["B"].value_counts()

Error:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/username/.virtualenvs/my_project/lib/python3.8/site-packages/pandas/core/groupby/generic.py", line 736, in value_counts
    codes = [rep(level_codes) for level_codes in codes] + [llab(lab, inc)]
  File "/Users/username/.virtualenvs/my_project/lib/python3.8/site-packages/pandas/core/groupby/generic.py", line 705, in <lambda>
    llab = lambda lab, inc: lab[inc]
IndexError: boolean index did not match indexed array along dimension 0; dimension is 0 but corresponding boolean dimension is 1

Instead, I would expect that value_counts returns an empty series, like when using the max method, like the following,

>>> pd.DataFrame(columns=["A", "B"]).groupby("A")["B"].max()

which gives this result:

Series([], Name: B, dtype: object)

If it is normal to have an error when using value_counts for an empty SeriesGroupBy, why is it so?

Thank you.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions