diff --git a/doc/source/v0.15.0.txt b/doc/source/v0.15.0.txt index 6e2d1d974429b..4f4f0211f130c 100644 --- a/doc/source/v0.15.0.txt +++ b/doc/source/v0.15.0.txt @@ -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 ` and the :ref:`API documentation `. diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 1e85bf82c6bc0..7efcfb9898053 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -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 np.asarray(cat)'." raise TypeError(msg.format(op=op,typ=type(y))) diff --git a/pandas/lib.pyx b/pandas/lib.pyx index 7ffc59f6ab50d..07b1efcd834db 100644 --- a/pandas/lib.pyx +++ b/pandas/lib.pyx @@ -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] diff --git a/pandas/tests/test_categorical.py b/pandas/tests/test_categorical.py index 7bc2eeb97d47a..403f2e9329f95 100644 --- a/pandas/tests/test_categorical.py +++ b/pandas/tests/test_categorical.py @@ -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