Skip to content

TST: Multiindex slicing with NaNs, unexpected results for #25154 #39356

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 7 commits into from
Feb 5, 2021
79 changes: 79 additions & 0 deletions pandas/tests/indexing/multiindex/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,85 @@ def test_frame_getitem_nan_multiindex(nulls_fixture):
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"indexer,expected",
[
(
(["b"], ["bar", np.nan]),
(
DataFrame(
[[2, 3], [5, 6]],
columns=MultiIndex.from_tuples([("b", "bar"), ("b", np.nan)]),
dtype="int64",
)
),
),
(
(["a", "b"]),
(
DataFrame(
[[1, 2, 3], [4, 5, 6]],
columns=MultiIndex.from_tuples(
[("a", "foo"), ("b", "bar"), ("b", np.nan)]
),
dtype="int64",
)
),
),
(
(["b"]),
(
DataFrame(
[[2, 3], [5, 6]],
columns=MultiIndex.from_tuples([("b", "bar"), ("b", np.nan)]),
dtype="int64",
)
),
),
(
(["b"], ["bar"]),
(
DataFrame(
[[2], [5]],
columns=MultiIndex.from_tuples([("b", "bar")]),
dtype="int64",
)
),
),
(
(["b"], [np.nan]),
(
DataFrame(
[[3], [6]],
columns=MultiIndex(
codes=[[1], [-1]], levels=[["a", "b"], ["bar", "foo"]]
),
dtype="int64",
)
),
),
(("b", np.nan), Series([3, 6], dtype="int64", name=("b", np.nan))),
],
)
def test_frame_getitem_nan_cols_multiindex(
indexer,
expected,
nulls_fixture,
):
# Slicing MultiIndex including levels with nan values, for more information
# see GH#25154
df = DataFrame(
[[1, 2, 3], [4, 5, 6]],
columns=MultiIndex.from_tuples(
[("a", "foo"), ("b", "bar"), ("b", nulls_fixture)]
),
dtype="int64",
)

result = df.loc[:, indexer]
tm.assert_equal(result, expected)


# ----------------------------------------------------------------------------
# test indexing of DataFrame with multi-level Index with duplicates
# ----------------------------------------------------------------------------
Expand Down