diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index a8684ca4d3c25..50097ae3787b3 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -525,38 +525,6 @@ def test_format_empty(self): assert empty_idx.format() == [] assert empty_idx.format(name=True) == [""] - def test_hasnans_isnans(self, index_flat): - # GH 11343, added tests for hasnans / isnans - index = index_flat - - # cases in indices doesn't include NaN - idx = index.copy(deep=True) - expected = np.array([False] * len(idx), dtype=bool) - tm.assert_numpy_array_equal(idx._isnan, expected) - assert idx.hasnans is False - - idx = index.copy(deep=True) - values = np.asarray(idx.values) - - if len(index) == 0: - return - elif isinstance(index, NumericIndex) and is_integer_dtype(index.dtype): - return - elif isinstance(index, DatetimeIndexOpsMixin): - values[1] = iNaT - else: - values[1] = np.nan - - if isinstance(index, PeriodIndex): - idx = type(index)(values, freq=index.freq) - else: - idx = type(index)(values) - - expected = np.array([False] * len(idx), dtype=bool) - expected[1] = True - tm.assert_numpy_array_equal(idx._isnan, expected) - assert idx.hasnans is True - def test_fillna(self, index): # GH 11343 if len(index) == 0: diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index 33aa8bbb942d5..604b68cfcc791 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -12,6 +12,7 @@ from pandas.compat import IS64 from pandas.core.dtypes.common import ( + is_integer_dtype, is_period_dtype, needs_i8_conversion, ) @@ -366,6 +367,38 @@ def test_asi8_deprecation(self, index): with tm.assert_produces_warning(warn): index.asi8 + def test_hasnans_isnans(self, index_flat): + # GH#11343, added tests for hasnans / isnans + index = index_flat + + # cases in indices doesn't include NaN + idx = index.copy(deep=True) + expected = np.array([False] * len(idx), dtype=bool) + tm.assert_numpy_array_equal(idx._isnan, expected) + assert idx.hasnans is False + + idx = index.copy(deep=True) + values = np.asarray(idx.values) + + if len(index) == 0: + return + elif isinstance(index, NumericIndex) and is_integer_dtype(index.dtype): + return + elif needs_i8_conversion(index.dtype): + values[1] = iNaT + else: + values[1] = np.nan + + if isinstance(index, PeriodIndex): + idx = type(index)(values, freq=index.freq) + else: + idx = type(index)(values) + + expected = np.array([False] * len(idx), dtype=bool) + expected[1] = True + tm.assert_numpy_array_equal(idx._isnan, expected) + assert idx.hasnans is True + @pytest.mark.parametrize("na_position", [None, "middle"]) def test_sort_values_invalid_na_position(index_with_missing, na_position):