A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pandas-dev/pandas/issues/8658 below:

Categoricals cannot compare to ndim=0 array values · Issue #8658 · pandas-dev/pandas · GitHub

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.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4