Skip to content

Fix #12564 for Categorical: consistent result if comparing as DataFrame #12698

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.18.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
11 changes: 11 additions & 0 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file modified pandas/tests/test_categorical.py
100755 → 100644
Empty file.