diff --git a/doc/source/whatsnew/v0.18.1.txt b/doc/source/whatsnew/v0.18.1.txt index f20b961455ba7..dc50eb94f7120 100644 --- a/doc/source/whatsnew/v0.18.1.txt +++ b/doc/source/whatsnew/v0.18.1.txt @@ -207,7 +207,9 @@ Bug Fixes - Bug in ``pivot_table`` when ``margins=True`` and ``dropna=True`` where nulls still contributed to margin count (:issue:`12577`) + - Bug in ``Series.name`` when ``name`` attribute can be a hashable type (:issue:`12610`) - Bug in ``.describe()`` resets categorical columns information (:issue:`11558`) - Bug where ``loffset`` argument was not applied when calling ``resample().count()`` on a timeseries (:issue:`12725`) - ``pd.read_excel()`` now accepts path objects (e.g. ``pathlib.Path``, ``py.path.local``) for the file path, in line with other ``read_*`` functions (:issue:`12655`) +- Bug in ``CategoricalBlock`` when Categoricals equality check raises ValueError in DataFrame (:issue:`12564`) diff --git a/pandas/core/internals.py b/pandas/core/internals.py index a31bd347e674a..9f4d4f7aa2e51 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -1874,6 +1874,17 @@ def _slice(self, slicer): # return same dims as we currently have return self.values._slice(slicer) + def _try_coerce_result(self, result): + """ reverse of try_coerce_args """ + + # fix issue #12564: CategoricalBlock is 1-dim only, while category + # datarame can be more. + if ((not com.is_categorical_dtype(result)) and + isinstance(result, np.ndarray)): + result = _block_shape(result, ndim=self.ndim) + + return result + def fillna(self, value, limit=None, inplace=False, downcast=None, mgr=None): # we may need to upcast our fill to match our dtype diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py index e5be2bb08f605..3650051e11886 100644 --- a/pandas/tests/indexing/test_indexing.py +++ b/pandas/tests/indexing/test_indexing.py @@ -936,6 +936,22 @@ def check(target, indexers, value, compare_fn, expected=None): check(target=df, indexers=(df.index, df.columns), value=df, compare_fn=assert_frame_equal, expected=copy) + def test_indexing_with_category(self): + + # https://github.com/pydata/pandas/issues/12564#issuecomment-194022792 + # consistent result if comparing as Dataframe + + cat = DataFrame({'A': ['foo', 'bar', 'baz']}) + exp = DataFrame({'A': [True, False, False]}) + + res = (cat[['A']] == 'foo') + tm.assert_frame_equal(res, exp) + + cat['A'] = cat['A'].astype('category') + + res = (cat[['A']] == 'foo') + tm.assert_frame_equal(res, exp) + def test_indexing_with_datetime_tz(self): # 8260 diff --git a/pandas/tests/test_categorical.py b/pandas/tests/test_categorical.py old mode 100755 new mode 100644