-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from 4 commits
a9a4ff4
c0cb975
da9a5f0
df14980
071256c
548ca8a
ada8989
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,6 @@ | |
[(0, Series([1], index=[0])), (1, Series([2, 3], index=[1, 2]))], | ||
) | ||
def test_series_getitem_multiindex(access_method, level1_value, expected): | ||
|
||
# GH 6018 | ||
# series regression getitem with a multi-index | ||
|
||
|
@@ -87,7 +86,8 @@ def test_series_getitem_returns_scalar( | |
(lambda s: s[(2000, 3, 4)], KeyError, r"^\(2000, 3, 4\)$"), | ||
(lambda s: s.loc[(2000, 3, 4)], KeyError, r"^\(2000, 3, 4\)$"), | ||
(lambda s: s.loc[(2000, 3, 4, 5)], IndexingError, "Too many indexers"), | ||
(lambda s: s.__getitem__(len(s)), KeyError, ""), # match should include len(s) | ||
(lambda s: s.__getitem__(len(s)), KeyError, ""), | ||
# match should include len(s) | ||
(lambda s: s[len(s)], KeyError, ""), # match should include len(s) | ||
( | ||
lambda s: s.iloc[len(s)], | ||
|
@@ -229,6 +229,86 @@ 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.from_tuples([("b", np.nan)]), | ||
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] | ||
if isinstance(result, DataFrame): | ||
tm.assert_frame_equal(result, expected, check_column_type=False) | ||
elif isinstance(result, Series): | ||
tm.assert_series_equal(result, expected) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use tm.assert_equal(...) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried, but unfortunately I couldn't pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use to create your columns There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the suggestion. Requested changes were implemented. |
||
|
||
|
||
# ---------------------------------------------------------------------------- | ||
# test indexing of DataFrame with multi-level Index with duplicates | ||
# ---------------------------------------------------------------------------- | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason you are doing this and removed the line above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was changed by accident. Sorry for missing it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I revert those changes in this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for pointing this out. Requested changes were implemented.