Skip to content

Commit d428ab6

Browse files
simonjayhawkinsbashtage
authored andcommitted
Update test_crosstab.py
1 parent df51abb commit d428ab6

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

pandas/core/reshape/pivot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ def _all_key(key):
329329
piece = piece.copy()
330330
try:
331331
piece[all_key] = margin[key]
332-
except (TypeError, ValueError):
332+
except ValueError:
333333
# we cannot reshape, so coerce the axis
334334
piece.set_axis(
335335
piece._get_axis(cat_axis)._to_safe_for_reshape(),

pandas/tests/reshape/test_crosstab.py

+12
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ def test_margin_normalize_multiple_columns(self):
746746

747747

748748
def test_categoricals():
749+
# https://github.com/pandas-dev/pandas/issues/37465
749750
g = np.random.RandomState(25982704)
750751
a = Series(g.randint(0, 3, size=100)).astype("category")
751752
b = Series(g.randint(0, 2, size=100)).astype("category")
@@ -755,3 +756,14 @@ def test_categoricals():
755756
values = [[18, 16, 34], [18, 16, 34], [16, 16, 32], [52, 48, 100]]
756757
expected = DataFrame(values, index, columns)
757758
tm.assert_frame_equal(result, expected)
759+
760+
# Verify when categorical does not have all values present
761+
g = np.random.RandomState(25982704)
762+
a = Series(g.randint(0, 3, size=100)).astype("category")
763+
a.loc[a == 1] = 2
764+
assert a.value_counts().loc[1] == 0
765+
b = Series(g.randint(0, 2, size=100)).astype("category")
766+
result = crosstab(a, b, margins=True, dropna=False)
767+
values = [[18, 16, 34], [0, 0, np.nan], [34, 32, 66], [52, 48, 100]]
768+
expected = DataFrame(values, index, columns)
769+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)