Skip to content

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

Closed
@simonjayhawkins

Description

@simonjayhawkins

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugCategoricalCategorical Data TypeError ReportingIncorrect or improved errors from pandasRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions