-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
Closed
Labels
Milestone
Description
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)
<ipython-input-3-5c4bb0678680> in <module>()
----> 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) <op> 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) <op> 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.