Skip to content

Commit 01217bc

Browse files
committed
add whatsnew entry, update comment and unit tests in test_crosstab.py
1 parent df57d87 commit 01217bc

File tree

3 files changed

+4
-21
lines changed

3 files changed

+4
-21
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,7 @@ Reshaping
11251125
- Bug in :meth:`Series.where` with an empty Series and empty ``cond`` having non-bool dtype (:issue:`34592`)
11261126
- Fixed regression where :meth:`DataFrame.apply` would raise ``ValueError`` for elements whth ``S`` dtype (:issue:`34529`)
11271127
- Bug in :meth:`DataFrame.append` leading to sorting columns even when ``sort=False`` is specified (:issue:`35092`)
1128+
- Bug in func :meth:`crosstab` when using multiple columns with ``margins=True`` and ``normalize=True`` (:issue:`35144`)
11281129

11291130
Sparse
11301131
^^^^^^

pandas/core/reshape/pivot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ def _normalize(table, normalize, margins: bool, margins_name="All"):
671671
table_index = table.index
672672
table_columns = table.columns
673673

674-
# check if margin name is in (for MI cases) or equal to last
674+
# check if margin name is not in (for MI cases) and not equal to last
675675
# index/column and save the column and index margin
676676
if (margins_name not in table.iloc[-1, :].name) & (
677677
margins_name != table.iloc[:, -1].name

pandas/tests/reshape/test_crosstab.py

+2-20
Original file line numberDiff line numberDiff line change
@@ -699,26 +699,8 @@ def test_margin_normalize(self):
699699
)
700700
tm.assert_frame_equal(result, expected)
701701

702-
def test_crosstab_multiple_columns_normalize(self):
703-
df = DataFrame(
704-
{
705-
"A": ["foo", "foo", "foo", "foo", "foo", "bar", "bar", "bar", "bar"],
706-
"B": ["one", "one", "one", "two", "two", "one", "one", "two", "two"],
707-
"C": [
708-
"small",
709-
"large",
710-
"large",
711-
"small",
712-
"small",
713-
"large",
714-
"small",
715-
"small",
716-
"large",
717-
],
718-
"D": [1, 2, 2, 3, 3, 4, 5, 6, 7],
719-
"E": [2, 4, 5, 5, 6, 6, 8, 9, 9],
720-
}
721-
)
702+
# GH 35144
703+
# use multiple columns with margins and normalization
722704
result = crosstab(
723705
index=df.C,
724706
columns=[df.A, df.B],

0 commit comments

Comments
 (0)