Skip to content

Commit 249d93e

Browse files
TST: crosstab margins with ordered categorical column (#52645)
TST: crosstab margin with ordered categorical column
1 parent 5799de2 commit 249d93e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

pandas/tests/reshape/test_crosstab.py

+21
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,27 @@ def test_margin_support_Float(self):
818818
)
819819
tm.assert_frame_equal(result, expected)
820820

821+
def test_margin_with_ordered_categorical_column(self):
822+
# GH 25278
823+
df = DataFrame(
824+
{
825+
"First": ["B", "B", "C", "A", "B", "C"],
826+
"Second": ["C", "B", "B", "B", "C", "A"],
827+
}
828+
)
829+
df["First"] = df["First"].astype(CategoricalDtype(ordered=True))
830+
customized_categories_order = ["C", "A", "B"]
831+
df["First"] = df["First"].cat.reorder_categories(customized_categories_order)
832+
result = crosstab(df["First"], df["Second"], margins=True)
833+
834+
expected_index = Index(["C", "A", "B", "All"], name="First")
835+
expected_columns = Index(["A", "B", "C", "All"], name="Second")
836+
expected_data = [[1, 1, 0, 2], [0, 1, 0, 1], [0, 1, 2, 3], [1, 3, 2, 6]]
837+
expected = DataFrame(
838+
expected_data, index=expected_index, columns=expected_columns
839+
)
840+
tm.assert_frame_equal(result, expected)
841+
821842

822843
@pytest.mark.parametrize("a_dtype", ["category", "int64"])
823844
@pytest.mark.parametrize("b_dtype", ["category", "int64"])

0 commit comments

Comments
 (0)