Skip to content

add test to check crosstab supports Float64 formats #50459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 28, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions pandas/tests/reshape/test_crosstab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you avoid the astype and define this as float immediately?

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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, please define expected in one go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @phofl, for your comments. I updated my PR as you suggested.

expected.index.name = "A"
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("a_dtype", ["category", "int64"])
@pytest.mark.parametrize("b_dtype", ["category", "int64"])
Expand Down