Skip to content

Commit 6cab622

Browse files
committed
Fix pandas-dev#12564 for Categorical: consistent result if comparing as DataFrame
1 parent 101d81d commit 6cab622

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
@@ -207,7 +207,9 @@ Bug Fixes
207207

208208

209209
- Bug in ``pivot_table`` when ``margins=True`` and ``dropna=True`` where nulls still contributed to margin count (:issue:`12577`)
210+
210211
- Bug in ``Series.name`` when ``name`` attribute can be a hashable type (:issue:`12610`)
211212
- Bug in ``.describe()`` resets categorical columns information (:issue:`11558`)
212213
- Bug where ``loffset`` argument was not applied when calling ``resample().count()`` on a timeseries (:issue:`12725`)
213214
- ``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`)
215+
- Bug in ``CategoricalBlock`` when Categoricals equality check raises ValueError in DataFrame (:issue:`12564`)

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+
# fix issue #12564: CategoricalBlock is 1-dim only, while category
1881+
# datarame can be more.
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_indexing.py

+16
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,22 @@ def check(target, indexers, value, compare_fn, expected=None):
936936
check(target=df, indexers=(df.index, df.columns), value=df,
937937
compare_fn=assert_frame_equal, expected=copy)
938938

939+
def test_indexing_with_category(self):
940+
941+
# https://github.com/pydata/pandas/issues/12564#issuecomment-194022792
942+
# consistent result if comparing as Dataframe
943+
944+
cat = DataFrame({'A': ['foo', 'bar', 'baz']})
945+
exp = DataFrame({'A': [True, False, False]})
946+
947+
res = (cat[['A']] == 'foo')
948+
tm.assert_frame_equal(res, exp)
949+
950+
cat['A'] = cat['A'].astype('category')
951+
952+
res = (cat[['A']] == 'foo')
953+
tm.assert_frame_equal(res, exp)
954+
939955
def test_indexing_with_datetime_tz(self):
940956

941957
# 8260

pandas/tests/test_categorical.py

100755100644
File mode changed.

0 commit comments

Comments
 (0)