BUG: Categoricals cannot compare to ndim=0 array values · Issue #8658 · pandas-dev/pandas (original) (raw)

Reproduction steps:

In [1]: cat = pd.Categorical([1,2,3])

In [2]: cat > cat[0] Out[2]: array([False, True, True], dtype=bool)

In [3]: cat[0] < cat

TypeError Traceback (most recent call last) in () ----> 1 cat[0] < cat

/home/dshpektorov/sources/pandas/pandas/core/categorical.pyc in f(self, other) 52 msg = "Cannot compare a Categorical for op {op} with type {typ}. If you want to \n"
53 "compare values, use 'np.asarray(cat) other'." ---> 54 raise TypeError(msg.format(op=op,typ=type(other))) 55 56 f.name = op

TypeError: Cannot compare a Categorical for op gt with type <type 'numpy.ndarray'>. If you want to compare values, use 'np.asarray(cat) other'.

The problem is that when cat[0] is the first operand (type == np.int64) it orchestrates the execution and the first step is casting it to a rank-0 array (it becomes array(1) rather than np.int64(1)) and at that point it ceases to pass np.isscalar check and corresponding branch of execution is skipped.

The fix is trivial, feel free to beat me to it.

On a broader scale, we might want to review all uses of np.isscalar and see if they can also benefit from accepting rank-0 arrays. If that's true, we might want to add pd.isscalar function and use it everywhere instead.