Skip to content

Commit

Permalink
BUG: Categorical equality check with a DataFrame
Browse files Browse the repository at this point in the history
closes #12564
closes #12698
  • Loading branch information
facaiy authored and jreback committed Apr 3, 2016
1 parent 101d81d commit ad8ade8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.18.1.txt
Expand Up @@ -172,6 +172,7 @@ Bug Fixes



- Bug in equality testing with a ``Categorical`` in a ``DataFrame`` (:issue:`12564`)



Expand Down Expand Up @@ -207,6 +208,7 @@ 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`)
Expand Down
11 changes: 11 additions & 0 deletions pandas/core/internals.py
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 """

# GH12564: CategoricalBlock is 1-dim only
# while returned results could be any dim
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_categorical.py
Expand Up @@ -382,3 +382,19 @@ def test_boolean_selection(self):
# name=u'B')
self.assertRaises(TypeError, lambda: df4[df4.index < 2])
self.assertRaises(TypeError, lambda: df4[df4.index > 1])

def test_indexing_with_category(self):

# https://github.com/pydata/pandas/issues/12564
# 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)
Empty file modified pandas/tests/test_categorical.py 100755 → 100644
Empty file.

0 comments on commit ad8ade8

Please sign in to comment.