Skip to content

Commit 03450d9

Browse files
committed
BUG: Added test cases to check loc on a multiindex with nan values pandas-dev#29751
1 parent 7f912a4 commit 03450d9

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

pandas/tests/indexing/multiindex/test_getitem.py

+30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
import pytest
33

4+
import pandas as pd
45
from pandas import DataFrame, Index, MultiIndex, Series
56
import pandas._testing as tm
67
from pandas.core.indexing import IndexingError
@@ -279,3 +280,32 @@ def test_loc_empty_multiindex():
279280
result = df
280281
expected = DataFrame([1, 2, 3, 4], index=index, columns=["value"])
281282
tm.assert_frame_equal(result, expected)
283+
284+
285+
@pytest.mark.parametrize("nan", [np.nan, pd.NA, None])
286+
def test_loc_nan_multiindex(nan):
287+
# GH#29751
288+
# tests to check loc on a multiindex containing nan values
289+
arr = [
290+
[11, nan, 13],
291+
[21, nan, 23],
292+
[31, nan, 33],
293+
[41, nan, 43],
294+
[51, nan, 53],
295+
]
296+
cols = ["a", "b", "c"]
297+
df = DataFrame(arr, columns=cols, dtype="int").set_index(["a", "b"])
298+
idx = df.index[1]
299+
result = df.loc[:idx, :]
300+
expected = DataFrame(arr[:2], columns=cols, dtype="int").set_index(["a", "b"])
301+
tm.assert_frame_equal(result, expected)
302+
303+
result = df.loc[idx:, :]
304+
expected = DataFrame(arr[1:], columns=cols).set_index(["a", "b"])
305+
tm.assert_frame_equal(result, expected)
306+
307+
idx1 = df.index[1]
308+
idx2 = df.index[3]
309+
result = df.loc[idx1:idx2, :]
310+
expected = DataFrame(arr[1:4], columns=cols).set_index(["a", "b"])
311+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)