Skip to content

BUG: min/max on empty categorical fails #30227

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

Conversation

OliverHofkens
Copy link

  • tests added / passed
  • passes black pandas
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff
  • whatsnew entry

Currently on master, the following fails:

import pandas as pd

# Empty Dataframe
df = pd.DataFrame({
    "cat": pd.Categorical([], categories=list("ABC"), ordered=True),
    "val": pd.Series([], dtype="int64")
})

df["cat"].max()

ValueError: zero-size array to reduction operation maximum which has no identity

Other dtypes return NaN when calling min() or max() on an empty Series. This PR changes the behavior of Categoricals to be the same.

Note that current behavior causes a downstream bug in Dask here: dask/dask#5645, so this PR could fix that bug as well.

@topper-123 topper-123 added Categorical Categorical Data Type Bug labels Dec 12, 2019
@topper-123 topper-123 added this to the 1.0 milestone Dec 12, 2019
Copy link
Contributor

@topper-123 topper-123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@TomAugspurger TomAugspurger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a .. versionchanged to indicate that an NA value is returned for empty arrays.

@TomAugspurger TomAugspurger merged commit 68b3eb4 into pandas-dev:master Dec 17, 2019
@TomAugspurger
Copy link
Contributor

Thanks!

@OliverHofkens OliverHofkens deleted the fix/minmax-on-empty-categorical branch December 18, 2019 07:15
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants