|
12 | 12 | from pandas.compat import IS64
|
13 | 13 |
|
14 | 14 | from pandas.core.dtypes.common import (
|
| 15 | + is_integer_dtype, |
15 | 16 | is_period_dtype,
|
16 | 17 | needs_i8_conversion,
|
17 | 18 | )
|
@@ -366,6 +367,38 @@ def test_asi8_deprecation(self, index):
|
366 | 367 | with tm.assert_produces_warning(warn):
|
367 | 368 | index.asi8
|
368 | 369 |
|
| 370 | + def test_hasnans_isnans(self, index_flat): |
| 371 | + # GH#11343, added tests for hasnans / isnans |
| 372 | + index = index_flat |
| 373 | + |
| 374 | + # cases in indices doesn't include NaN |
| 375 | + idx = index.copy(deep=True) |
| 376 | + expected = np.array([False] * len(idx), dtype=bool) |
| 377 | + tm.assert_numpy_array_equal(idx._isnan, expected) |
| 378 | + assert idx.hasnans is False |
| 379 | + |
| 380 | + idx = index.copy(deep=True) |
| 381 | + values = np.asarray(idx.values) |
| 382 | + |
| 383 | + if len(index) == 0: |
| 384 | + return |
| 385 | + elif isinstance(index, NumericIndex) and is_integer_dtype(index.dtype): |
| 386 | + return |
| 387 | + elif needs_i8_conversion(index.dtype): |
| 388 | + values[1] = iNaT |
| 389 | + else: |
| 390 | + values[1] = np.nan |
| 391 | + |
| 392 | + if isinstance(index, PeriodIndex): |
| 393 | + idx = type(index)(values, freq=index.freq) |
| 394 | + else: |
| 395 | + idx = type(index)(values) |
| 396 | + |
| 397 | + expected = np.array([False] * len(idx), dtype=bool) |
| 398 | + expected[1] = True |
| 399 | + tm.assert_numpy_array_equal(idx._isnan, expected) |
| 400 | + assert idx.hasnans is True |
| 401 | + |
369 | 402 |
|
370 | 403 | @pytest.mark.parametrize("na_position", [None, "middle"])
|
371 | 404 | def test_sort_values_invalid_na_position(index_with_missing, na_position):
|
|
0 commit comments