Skip to content

Commit 31db27e

Browse files
committed
BUG: Respect filtered Categorical in crosstab
Closes gh-12298. [ci skip]
1 parent 4de83d2 commit 31db27e

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

doc/source/whatsnew/v0.18.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ Performance Improvements
108108
Bug Fixes
109109
~~~~~~~~~
110110
- Bug in ``SparseDataFrame`` in which ``axis=None`` did not default to ``axis=0`` (:issue:`13048`)
111+
- ``pd.crosstab`` now respects filtered ``Categorical`` objects (:issue:`12298`)
111112

112113

113114

pandas/tools/pivot.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,15 @@ def crosstab(index, columns, values=None, rownames=None, colnames=None,
439439
crosstab : DataFrame
440440
"""
441441

442-
index = com._maybe_make_list(index)
443-
columns = com._maybe_make_list(columns)
442+
def _make_list(arr):
443+
# see gh-12298
444+
if com.is_categorical(arr):
445+
arr = Series(list(arr), name=arr.name)
446+
447+
return com._maybe_make_list(arr)
448+
449+
index = _make_list(index)
450+
columns = _make_list(columns)
444451

445452
rownames = _get_names(index, rownames, prefix='row')
446453
colnames = _get_names(columns, colnames, prefix='col')

pandas/tools/tests/test_pivot.py

+23
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,29 @@ def test_crosstab_with_empties(self):
11391139
normalize=False)
11401140
tm.assert_frame_equal(nans, calculated)
11411141

1142+
def test_crosstab_filtered_categorical(self):
1143+
# see gh-12298
1144+
df = pd.DataFrame({'col0': list('abcabc'),
1145+
'col1': [1, 1, 2, 1, 2, 3],
1146+
'col2': [1, 1, 0, 1, 1, 0]})
1147+
data = [[2, 0], [1, 1]]
1148+
columns = pd.Index([1, 2], name='col1')
1149+
index = pd.Index(['a', 'b'], name='col0')
1150+
expected = pd.DataFrame(data, columns=columns, index=index)
1151+
1152+
# sanity check
1153+
filtered = df[df.col2 == 1]
1154+
result = pd.crosstab(filtered.col0, filtered.col1)
1155+
tm.assert_frame_equal(result, expected)
1156+
1157+
# casting columns to Categorical shouldn't change anything
1158+
for col in df.columns:
1159+
df[col] = df[col].astype('category')
1160+
1161+
filtered = df[df.col2 == 1]
1162+
result = pd.crosstab(filtered.col0, filtered.col1)
1163+
tm.assert_frame_equal(result, expected)
1164+
11421165
def test_crosstab_errors(self):
11431166
# Issue 12578
11441167

0 commit comments

Comments
 (0)