Skip to content

BUG: Added test cases to check loc on multiindex with NaNs #29751 #38772

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 6 commits into from
Jan 7, 2021
30 changes: 30 additions & 0 deletions pandas/tests/indexing/multiindex/test_getitem.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import pytest

import pandas as pd
from pandas import DataFrame, Index, MultiIndex, Series
import pandas._testing as tm
from pandas.core.indexing import IndexingError
Expand Down Expand Up @@ -279,3 +280,32 @@ def test_loc_empty_multiindex():
result = df
expected = DataFrame([1, 2, 3, 4], index=index, columns=["value"])
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("nan", [np.nan, pd.NA, None])
Copy link
Member

Choose a reason for hiding this comment

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

There is a fixture for all NaNs

Copy link
Contributor

Choose a reason for hiding this comment

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

nulls_fixture

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I fixed the test case to use nulls_fixture in the most recent commit

def test_loc_nan_multiindex(nan):
# GH#29751
# tests to check loc on a multiindex containing nan values
arr = [
[11, nan, 13],
[21, nan, 23],
[31, nan, 33],
[41, nan, 43],
[51, nan, 53],
]
cols = ["a", "b", "c"]
df = DataFrame(arr, columns=cols, dtype="int").set_index(["a", "b"])
Copy link
Contributor

Choose a reason for hiding this comment

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

use dtype='int64' to avoid the 32-bit failures

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed dtype to int64 in the recent commit

idx = df.index[1]
result = df.loc[:idx, :]
expected = DataFrame(arr[:2], columns=cols, dtype="int").set_index(["a", "b"])
tm.assert_frame_equal(result, expected)

result = df.loc[idx:, :]
Copy link
Member

Choose a reason for hiding this comment

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

Could you try to parametrize with slices?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have parametrized the slices in the recent commit

expected = DataFrame(arr[1:], columns=cols).set_index(["a", "b"])
tm.assert_frame_equal(result, expected)

idx1 = df.index[1]
idx2 = df.index[3]
result = df.loc[idx1:idx2, :]
expected = DataFrame(arr[1:4], columns=cols).set_index(["a", "b"])
tm.assert_frame_equal(result, expected)