From 238cba5e7dce14202124fdd977a203b4a63d3c74 Mon Sep 17 00:00:00 2001 From: Tolker-KU Date: Sun, 10 Dec 2023 20:35:32 +0100 Subject: [PATCH] Test .groupby() encodes NaNs correctly --- pandas/tests/groupby/test_indexing.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pandas/tests/groupby/test_indexing.py b/pandas/tests/groupby/test_indexing.py index 664c52babac13..3a5341c2f0952 100644 --- a/pandas/tests/groupby/test_indexing.py +++ b/pandas/tests/groupby/test_indexing.py @@ -331,3 +331,15 @@ def test_groupby_get_nonexisting_groups(): msg = "('a2', 'b1')" with pytest.raises(KeyError, match=msg): grps.get_group(("a2", "b1")) + + +def test_groupby_multiindex_nan_encoding(): + # GH#54347 + df = pd.DataFrame( + {"A": [1, 2, 3, 4], "B": [1, float("nan"), 2, float("nan")], "C": [2, 4, 6, 8]} + ) + + df_grouped = df.groupby(["A", "B"], dropna=False).sum() + + index = df_grouped.index + tm.assert_index_equal(index, pd.MultiIndex.from_frame(index.to_frame()))