From a1099a4213b0dd6b77f578c0c8bfe8df76b02542 Mon Sep 17 00:00:00 2001 From: Tong Shen Date: Sun, 11 Jun 2017 21:39:42 +0800 Subject: [PATCH 1/2] BUG: Fix Categorical comparsion with Series of dtype 'category' This is a fix attempt for issue #16659. --- doc/source/whatsnew/v0.21.0.txt | 1 + pandas/core/categorical.py | 15 +++++++++++++-- pandas/tests/test_categorical.py | 5 +++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 89e2d3006696c..01ead1f9945e0 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -1168,6 +1168,7 @@ PyPy - Fix :func:`DataFrame.memory_usage` to support PyPy. Objects on PyPy do not have a fixed size, so an approximation is used instead (:issue:`17228`) + Other ^^^^^ - Bug where some inplace operators were not being wrapped and produced a copy when invoked (:issue:`12962`) diff --git a/pandas/core/categorical.py b/pandas/core/categorical.py index 1e3c8f89c0e05..ac7362776d346 100644 --- a/pandas/core/categorical.py +++ b/pandas/core/categorical.py @@ -2122,15 +2122,26 @@ def is_dtype_equal(self, other): Parameters ---------- - other : Categorical + other : Categorical, Series Returns ------- are_equal : boolean """ - try: +<<<<<<< 965c1c89b6df471d88dc0e1188fb8cbc0d89f867 return hash(self.dtype) == hash(other.dtype) +======= + from pandas.core.series import Series + + if isinstance(other, Series): + other_categorical = other.values + else: + other_categorical = other + + return (self.categories.equals(other_categorical.categories) and + self.ordered == other_categorical.ordered) +>>>>>>> BUG: Fix Categorical comparsion with Series of dtype 'category' except (AttributeError, TypeError): return False diff --git a/pandas/tests/test_categorical.py b/pandas/tests/test_categorical.py index b77e2d1dcda8a..6224dd79c2688 100644 --- a/pandas/tests/test_categorical.py +++ b/pandas/tests/test_categorical.py @@ -202,6 +202,11 @@ def test_is_equal_dtype(self): CategoricalIndex(c1, categories=list('cab')))) assert not c1.is_dtype_equal(CategoricalIndex(c1, ordered=True)) + s1 = pd.Series(c1) + assert c1.is_dtype_equal(s1) + assert not c2.is_dtype_equal(s1) + assert not c3.is_dtype_equal(s1) + def test_constructor(self): exp_arr = np.array(["a", "b", "c", "a", "b", "c"], dtype=np.object_) From 2771e5716a5435ba6d022882a206e267def4c0bd Mon Sep 17 00:00:00 2001 From: Tong Shen Date: Mon, 12 Jun 2017 00:53:30 +0800 Subject: [PATCH 2/2] simplify the fix, add issue reference number to corresponding test and tighten the wording in doc whatsnew --- pandas/core/categorical.py | 9 ++++++--- pandas/tests/test_categorical.py | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pandas/core/categorical.py b/pandas/core/categorical.py index ac7362776d346..e916be3396521 100644 --- a/pandas/core/categorical.py +++ b/pandas/core/categorical.py @@ -2135,13 +2135,16 @@ def is_dtype_equal(self, other): from pandas.core.series import Series if isinstance(other, Series): - other_categorical = other.values - else: - other_categorical = other + other = Categorical(other) +<<<<<<< a581a743fe6740011e4fb0a7031ee92ce57b480b return (self.categories.equals(other_categorical.categories) and self.ordered == other_categorical.ordered) >>>>>>> BUG: Fix Categorical comparsion with Series of dtype 'category' +======= + return (self.categories.equals(other.categories) and + self.ordered == other.ordered) +>>>>>>> simplify the fix, add issue reference number to corresponding test and tighten the wording in doc whatsnew except (AttributeError, TypeError): return False diff --git a/pandas/tests/test_categorical.py b/pandas/tests/test_categorical.py index 6224dd79c2688..17db84a88ae0d 100644 --- a/pandas/tests/test_categorical.py +++ b/pandas/tests/test_categorical.py @@ -202,6 +202,7 @@ def test_is_equal_dtype(self): CategoricalIndex(c1, categories=list('cab')))) assert not c1.is_dtype_equal(CategoricalIndex(c1, ordered=True)) + # GH 16659 s1 = pd.Series(c1) assert c1.is_dtype_equal(s1) assert not c2.is_dtype_equal(s1)