From 05e602099b2e94372c82e5d58256424ffb249828 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Wed, 28 Dec 2022 01:27:32 +0100 Subject: [PATCH 1/2] add test to check crosstab supports Float64 formats --- pandas/tests/reshape/test_crosstab.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pandas/tests/reshape/test_crosstab.py b/pandas/tests/reshape/test_crosstab.py index 76448d5942a5a..0ef8666afac2d 100644 --- a/pandas/tests/reshape/test_crosstab.py +++ b/pandas/tests/reshape/test_crosstab.py @@ -795,6 +795,33 @@ def test_margin_normalize_multiple_columns(self): expected.index.name = "C" tm.assert_frame_equal(result, expected) + def test_margin_support_Float(self): + # GH 50313 + # use Float64 formats and function aggfunc with margins + df = DataFrame( + {"A": [1, 2, 2, 1], "B": [3, 3, 4, 5], "C": [-1.0, 10.0, 1.0, 10.0]} + ) + df = df.astype({"C": "Float64"}) + result = crosstab( + df["A"], + df["B"], + values=df["C"], + aggfunc="sum", + margins=True, + ) + expected = DataFrame( + [ + [-1.0, pd.NA, 10.0, 9.0], + [10.0, 1.0, pd.NA, 11.0], + [9.0, 1.0, 10.0, 20.0], + ], + index=[1, 2, "All"], + dtype="Float64", + ) + expected.columns = Index([3, 4, 5, "All"], dtype="object", name="B") + expected.index.name = "A" + tm.assert_frame_equal(result, expected) + @pytest.mark.parametrize("a_dtype", ["category", "int64"]) @pytest.mark.parametrize("b_dtype", ["category", "int64"]) From a16f3ff91e14432fa6db6aec42379f4a70e24adc Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Wed, 28 Dec 2022 16:50:21 +0100 Subject: [PATCH 2/2] fixup! add test to check crosstab supports Float64 formats --- pandas/tests/reshape/test_crosstab.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pandas/tests/reshape/test_crosstab.py b/pandas/tests/reshape/test_crosstab.py index 0ef8666afac2d..bea0ddbe8a667 100644 --- a/pandas/tests/reshape/test_crosstab.py +++ b/pandas/tests/reshape/test_crosstab.py @@ -799,9 +799,9 @@ def test_margin_support_Float(self): # GH 50313 # use Float64 formats and function aggfunc with margins df = DataFrame( - {"A": [1, 2, 2, 1], "B": [3, 3, 4, 5], "C": [-1.0, 10.0, 1.0, 10.0]} + {"A": [1, 2, 2, 1], "B": [3, 3, 4, 5], "C": [-1.0, 10.0, 1.0, 10.0]}, + dtype="Float64", ) - df = df.astype({"C": "Float64"}) result = crosstab( df["A"], df["B"], @@ -815,11 +815,10 @@ def test_margin_support_Float(self): [10.0, 1.0, pd.NA, 11.0], [9.0, 1.0, 10.0, 20.0], ], - index=[1, 2, "All"], + index=Index([1.0, 2.0, "All"], dtype="object", name="A"), + columns=Index([3.0, 4.0, 5.0, "All"], dtype="object", name="B"), dtype="Float64", ) - expected.columns = Index([3, 4, 5, "All"], dtype="object", name="B") - expected.index.name = "A" tm.assert_frame_equal(result, expected)