Skip to content

Commit 71bfb00

Browse files
committed
Merge pull request #8146 from jreback/cat_dtype
BUG: comparison of category vs np dtypes buggy (GH8143)
2 parents 790840b + 3fe6a24 commit 71bfb00

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

doc/source/v0.15.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,8 @@ Categoricals in Series/DataFrame
347347

348348
:class:`~pandas.Categorical` can now be included in `Series` and `DataFrames` and gained new
349349
methods to manipulate. Thanks to Jan Schultz for much of this API/implementation. (:issue:`3943`, :issue:`5313`, :issue:`5314`,
350-
:issue:`7444`, :issue:`7839`, :issue:`7848`, :issue:`7864`, :issue:`7914`, :issue:`7768`, :issue:`8006`, :issue:`3678`, :issue:`8075`, :issue:`8076`).
350+
:issue:`7444`, :issue:`7839`, :issue:`7848`, :issue:`7864`, :issue:`7914`, :issue:`7768`, :issue:`8006`, :issue:`3678`,
351+
:issue:`8075`, :issue:`8076`, :issue:`8143`).
351352

352353
For full docs, see the :ref:`Categorical introduction <categorical>` and the
353354
:ref:`API documentation <api.categorical>`.

pandas/core/ops.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,8 @@ def _comp_method_SERIES(op, name, str_rep, masker=False):
522522
code duplication.
523523
"""
524524
def na_op(x, y):
525-
if com.is_categorical_dtype(x) != com.is_categorical_dtype(y):
525+
526+
if com.is_categorical_dtype(x) != (not np.isscalar(y) and com.is_categorical_dtype(y)):
526527
msg = "Cannot compare a Categorical for op {op} with type {typ}. If you want to \n" \
527528
"compare values, use 'series <op> np.asarray(cat)'."
528529
raise TypeError(msg.format(op=op,typ=type(y)))

pandas/lib.pyx

+15-1
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,21 @@ def scalar_compare(ndarray[object] values, object val, object op):
647647
if _checknull(x):
648648
result[i] = True
649649
else:
650-
result[i] = cpython.PyObject_RichCompareBool(x, val, flag)
650+
try:
651+
result[i] = cpython.PyObject_RichCompareBool(x, val, flag)
652+
except (TypeError):
653+
result[i] = True
654+
elif flag == cpython.Py_EQ:
655+
for i in range(n):
656+
x = values[i]
657+
if _checknull(x):
658+
result[i] = False
659+
else:
660+
try:
661+
result[i] = cpython.PyObject_RichCompareBool(x, val, flag)
662+
except (TypeError):
663+
result[i] = False
664+
651665
else:
652666
for i in range(n):
653667
x = values[i]

pandas/tests/test_categorical.py

+19
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,25 @@ def f():
785785
self.assertFalse(dtype == np.str_)
786786
self.assertFalse(np.str_ == dtype)
787787

788+
# GH8143
789+
index = ['cat','obj','num']
790+
cat = pd.Categorical(['a', 'b', 'c'])
791+
obj = pd.Series(['a', 'b', 'c'])
792+
num = pd.Series([1, 2, 3])
793+
df = pd.concat([pd.Series(cat), obj, num], axis=1, keys=index)
794+
795+
result = df.dtypes == 'object'
796+
expected = Series([False,True,False],index=index)
797+
tm.assert_series_equal(result, expected)
798+
799+
result = df.dtypes == 'int64'
800+
expected = Series([False,False,True],index=index)
801+
tm.assert_series_equal(result, expected)
802+
803+
result = df.dtypes == 'category'
804+
expected = Series([True,False,False],index=index)
805+
tm.assert_series_equal(result, expected)
806+
788807
def test_basic(self):
789808

790809
# test basic creation / coercion of categoricals

0 commit comments

Comments
 (0)