Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f97f78d

Browse files
simonjayhawkinsbashtage
authored andcommittedOct 30, 2020
Update test_crosstab.py
1 parent df51abb commit f97f78d

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed
 

‎pandas/core/reshape/pivot.py

Lines changed: 1 addition & 1 deletion
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

Lines changed: 13 additions & 0 deletions
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,15 @@ 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+
g = np.random.RandomState(25982704)
761+
a = Series(g.randint(0, 3, size=100)).astype("category")
762+
a.loc[a == 1] = 2
763+
assert a.value_counts().loc[1] == 0
764+
b = Series(g.randint(0, 2, size=100)).astype("category")
765+
result = crosstab(a, b, margins=True, dropna=False)
766+
values = [[18.0, 16.0, 34.0], [0.0, 0.0, np.nan], [34.0, 32.0, 66.0], [52.0, 48.0, 100.0]]
767+
expected = DataFrame(values, index, columns)
768+
for i in range(2):
769+
expected[i] = expected[i].astype("int64")
770+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)
Please sign in to comment.