|
1 | 1 | import numpy as np
|
2 | 2 | import pytest
|
3 | 3 |
|
| 4 | +import pandas as pd |
4 | 5 | from pandas import DataFrame, Index, MultiIndex, Series
|
5 | 6 | import pandas._testing as tm
|
6 | 7 | from pandas.core.indexing import IndexingError
|
@@ -279,3 +280,32 @@ def test_loc_empty_multiindex():
|
279 | 280 | result = df
|
280 | 281 | expected = DataFrame([1, 2, 3, 4], index=index, columns=["value"])
|
281 | 282 | 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