Skip to content
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

REGR: unhelpful error message with np.min on unordered Categorical #33115

Closed
simonjayhawkins opened this issue Mar 29, 2020 · 2 comments · Fixed by #34053
Closed

REGR: unhelpful error message with np.min on unordered Categorical #33115

simonjayhawkins opened this issue Mar 29, 2020 · 2 comments · Fixed by #34053
Labels
Bug Categorical Categorical Data Type Error Reporting Incorrect or improved errors from pandas Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@simonjayhawkins
Copy link
Member

xref #28949

on master

>>> import numpy as np
>>> import pandas as pd
>>>
>>> pd.__version__
'1.1.0.dev0 1008.g60b0e9fbc'
>>>
>>> cat = pd.Categorical(["a", "b", "c", "b"], ordered=False)
>>> cat
[a, b, c, b]
Categories (3, object): [a, b, c]
>>>
>>> np.min(cat)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 6, in amin
  File "C:\Users\simon\Anaconda3\envs\pandas-dev\lib\site-packages\numpy\core\fromnumeric.py", line 2793, in amin
    keepdims=keepdims, initial=initial, where=where)
  File "C:\Users\simon\Anaconda3\envs\pandas-dev\lib\site-packages\numpy\core\fromnumeric.py", line 88, in _wrapreduction
    return reduction(axis=axis, out=out, **passkwargs)
  File "C:\Users\simon\pandas\pandas\util\_decorators.py", line 212, in wrapper
    return func(*args, **kwargs)
TypeError: min() got an unexpected keyword argument 'axis'

on 0.25.3

>>> import numpy as np
>>> import pandas as pd
>>>
>>> pd.__version__
'0.25.3'
>>>
>>> cat = pd.Categorical(["a", "b", "c", "b"], ordered=False)
>>> cat
[a, b, c, b]
Categories (3, object): [a, b, c]
>>>
>>> np.min(cat)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\simon\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 2618, in amin
    initial=initial)
  File "C:\Users\simon\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 84, in _wrapreduction
    return reduction(axis=axis, out=out, **passkwargs)
  File "C:\Users\simon\Anaconda3\lib\site-packages\pandas\core\arrays\categorical.py", line 2278, in min
    self.check_for_ordered("min")
  File "C:\Users\simon\Anaconda3\lib\site-packages\pandas\core\arrays\categorical.py", line 1586, in check_for_ordered
    "Categorical to an ordered one\n".format(op=op)
TypeError: Categorical is not ordered for operation min
you can use .as_ordered() to change the Categorical to an ordered one
@simonjayhawkins simonjayhawkins added Categorical Categorical Data Type Regression Functionality that used to work in a prior pandas version Error Reporting Incorrect or improved errors from pandas labels Mar 29, 2020
@simonjayhawkins
Copy link
Member Author

regression caused by #27929 (between 0.25.3 and 1.0)

37526c1 is the first bad commit
commit 37526c1
Author: Mak Sze Chun [email protected]
Date: Mon Dec 2 18:37:11 2019 0800

API/DEPR: Change default skipna behaviour   deprecate numeric_only in Categorical.min and max (#27929)

cc @makbigc

@topper-123
Copy link
Contributor

topper-123 commented May 8, 2020

I'm leaning on that this is more a bug in numpy.

numpy does in core/fromnumeric.py::_wrapreduction:

    return reduction(axis=axis, out=out, **passkwargs)

i.e. forcing parameters axis=None and out=None down to our function, requiring us to have those parameters also. Seems to make better sense to push axis and out into the passkwargs dict, but drop them there if they're None so downstream would not require axis and out parameters?

Of course that's beyond pandas to fix (and I may misunderstand _wrapreduction of course), and I agree that np.min(cat) should work, so I'm ok with doing a fix for this in pandas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type Error Reporting Incorrect or improved errors from pandas Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants