Skip to content

BUG: comparison of category vs np dtypes buggy (GH8143) #8146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/source/v0.15.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ Categoricals in Series/DataFrame

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

For full docs, see the :ref:`Categorical introduction <categorical>` and the
:ref:`API documentation <api.categorical>`.
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,8 @@ def _comp_method_SERIES(op, name, str_rep, masker=False):
code duplication.
"""
def na_op(x, y):
if com.is_categorical_dtype(x) != com.is_categorical_dtype(y):

if com.is_categorical_dtype(x) != (not np.isscalar(y) and com.is_categorical_dtype(y)):
msg = "Cannot compare a Categorical for op {op} with type {typ}. If you want to \n" \
"compare values, use 'series <op> np.asarray(cat)'."
raise TypeError(msg.format(op=op,typ=type(y)))
Expand Down
16 changes: 15 additions & 1 deletion pandas/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,21 @@ def scalar_compare(ndarray[object] values, object val, object op):
if _checknull(x):
result[i] = True
else:
result[i] = cpython.PyObject_RichCompareBool(x, val, flag)
try:
result[i] = cpython.PyObject_RichCompareBool(x, val, flag)
except (TypeError):
result[i] = True
elif flag == cpython.Py_EQ:
for i in range(n):
x = values[i]
if _checknull(x):
result[i] = False
else:
try:
result[i] = cpython.PyObject_RichCompareBool(x, val, flag)
except (TypeError):
result[i] = False

else:
for i in range(n):
x = values[i]
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,25 @@ def f():
self.assertFalse(dtype == np.str_)
self.assertFalse(np.str_ == dtype)

# GH8143
index = ['cat','obj','num']
cat = pd.Categorical(['a', 'b', 'c'])
obj = pd.Series(['a', 'b', 'c'])
num = pd.Series([1, 2, 3])
df = pd.concat([pd.Series(cat), obj, num], axis=1, keys=index)

result = df.dtypes == 'object'
expected = Series([False,True,False],index=index)
tm.assert_series_equal(result, expected)

result = df.dtypes == 'int64'
expected = Series([False,False,True],index=index)
tm.assert_series_equal(result, expected)

result = df.dtypes == 'category'
expected = Series([True,False,False],index=index)
tm.assert_series_equal(result, expected)

def test_basic(self):

# test basic creation / coercion of categoricals
Expand Down