Skip to content

Commit ad8ade8

Browse files
facaiyjreback
authored andcommitted
BUG: Categorical equality check with a DataFrame
closes #12564 closes #12698
1 parent 101d81d commit ad8ade8

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

doc/source/whatsnew/v0.18.1.txt

+2
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ Bug Fixes
172172

173173

174174

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

176177

177178

@@ -207,6 +208,7 @@ Bug Fixes
207208

208209

209210
- Bug in ``pivot_table`` when ``margins=True`` and ``dropna=True`` where nulls still contributed to margin count (:issue:`12577`)
211+
210212
- Bug in ``Series.name`` when ``name`` attribute can be a hashable type (:issue:`12610`)
211213
- Bug in ``.describe()`` resets categorical columns information (:issue:`11558`)
212214
- Bug where ``loffset`` argument was not applied when calling ``resample().count()`` on a timeseries (:issue:`12725`)

pandas/core/internals.py

+11
Original file line numberDiff line numberDiff line change
@@ -1874,6 +1874,17 @@ def _slice(self, slicer):
18741874
# return same dims as we currently have
18751875
return self.values._slice(slicer)
18761876

1877+
def _try_coerce_result(self, result):
1878+
""" reverse of try_coerce_args """
1879+
1880+
# GH12564: CategoricalBlock is 1-dim only
1881+
# while returned results could be any dim
1882+
if ((not com.is_categorical_dtype(result)) and
1883+
isinstance(result, np.ndarray)):
1884+
result = _block_shape(result, ndim=self.ndim)
1885+
1886+
return result
1887+
18771888
def fillna(self, value, limit=None, inplace=False, downcast=None,
18781889
mgr=None):
18791890
# we may need to upcast our fill to match our dtype

pandas/tests/indexing/test_categorical.py

+16
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,19 @@ def test_boolean_selection(self):
382382
# name=u'B')
383383
self.assertRaises(TypeError, lambda: df4[df4.index < 2])
384384
self.assertRaises(TypeError, lambda: df4[df4.index > 1])
385+
386+
def test_indexing_with_category(self):
387+
388+
# https://github.com/pydata/pandas/issues/12564
389+
# consistent result if comparing as Dataframe
390+
391+
cat = DataFrame({'A': ['foo', 'bar', 'baz']})
392+
exp = DataFrame({'A': [True, False, False]})
393+
394+
res = (cat[['A']] == 'foo')
395+
tm.assert_frame_equal(res, exp)
396+
397+
cat['A'] = cat['A'].astype('category')
398+
399+
res = (cat[['A']] == 'foo')
400+
tm.assert_frame_equal(res, exp)

pandas/tests/test_categorical.py

100755100644
File mode changed.

0 commit comments

Comments
 (0)