Skip to content

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

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

Closed
1 of 2 tasks
FilippoBovo opened this issue Jan 14, 2021 · 3 comments · Fixed by #39326
Closed
1 of 2 tasks

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

FilippoBovo opened this issue Jan 14, 2021 · 3 comments · Fixed by #39326
Assignees
Milestone

Comments

@FilippoBovo
Copy link

  • 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.

@rhshadrach
Copy link
Member

Thanks for the report. This looks like a bug to me. Further investigations and PRs to fix are welcome!

@rhshadrach rhshadrach added Bug Groupby and removed Needs Triage Issue that has not been reviewed by a pandas team member Usage Question labels Jan 15, 2021
@rhshadrach rhshadrach changed the title QST: Why does SeriesGroupBy.value_counts() raise an error for an empty SeriesGroupBy? BUG: SeriesGroupBy.value_counts() raises on an empty Series Jan 15, 2021
@rhshadrach rhshadrach added this to the Contributions Welcome milestone Jan 15, 2021
@AnnaDaglis
Copy link
Contributor

Take

@AnnaDaglis
Copy link
Contributor

@jreback jreback modified the milestones: Contributions Welcome, 1.3 Jan 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants