diff --git a/doc/source/whatsnew/v0.15.1.txt b/doc/source/whatsnew/v0.15.1.txt index 74e24a3a7a462..fcc371bb0e2c3 100644 --- a/doc/source/whatsnew/v0.15.1.txt +++ b/doc/source/whatsnew/v0.15.1.txt @@ -156,6 +156,7 @@ Bug Fixes - Bug in ``Categorical`` not created properly with ``Series.to_frame()`` (:issue:`8626`) - Bug in coercing in astype of a ``Categorical`` of a passed ``pd.Categorical`` (this now raises ``TypeError`` correctly), (:issue:`8626`) - Bug in ``cut``/``qcut`` when using ``Series`` and ``retbins=True`` (:issue:`8589`) +- Bug in comparing ``Categorical`` of datetime raising when being compared to a scalar datetime (:issue:`8687`) @@ -165,7 +166,7 @@ Bug Fixes -- Bug in numeric index operations of add/sub with Float/Index Index with numpy arrays (:issue:`8608` +- Bug in numeric index operations of add/sub with Float/Index Index with numpy arrays (:issue:`8608`) - Bug in setitem with empty indexer and unwanted coercion of dtypes (:issue:`8669`) diff --git a/pandas/core/categorical.py b/pandas/core/categorical.py index 00128bd977911..598b29bf77e47 100644 --- a/pandas/core/categorical.py +++ b/pandas/core/categorical.py @@ -4,7 +4,7 @@ from warnings import warn import types -from pandas import compat +from pandas import compat, lib from pandas.compat import u from pandas.core.algorithms import factorize @@ -42,7 +42,7 @@ def f(self, other): # In other series, the leads to False, so do that here too ret[na_mask] = False return ret - elif np.isscalar(other): + elif lib.isscalar(other): if other in self.categories: i = self.categories.get_loc(other) return getattr(self._codes, op)(i) diff --git a/pandas/tests/test_categorical.py b/pandas/tests/test_categorical.py index e47d8aaa52c9b..3b84d4aa34756 100644 --- a/pandas/tests/test_categorical.py +++ b/pandas/tests/test_categorical.py @@ -912,6 +912,11 @@ def test_deprecated_levels(self): self.assertFalse(LooseVersion(pd.__version__) >= '0.18') + def test_datetime_categorical_comparison(self): + dt_cat = pd.Categorical(pd.date_range('2014-01-01', periods=3)) + self.assert_numpy_array_equal(dt_cat > dt_cat[0], [False, True, True]) + self.assert_numpy_array_equal(dt_cat[0] < dt_cat, [False, True, True]) + class TestCategoricalAsBlock(tm.TestCase): _multiprocess_can_split_ = True