Skip to content

Commit 831501b

Browse files
committed
make hasnans always return python booleans
1 parent dae17ab commit 831501b

File tree

10 files changed

+21
-20
lines changed

10 files changed

+21
-20
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,7 @@ Other API Changes
904904
has an improved ``KeyError`` message, and will not fail on duplicate column names with ``drop=True``. (:issue:`22484`)
905905
- Slicing a single row of a DataFrame with multiple ExtensionArrays of the same type now preserves the dtype, rather than coercing to object (:issue:`22784`)
906906
- :class:`DateOffset` attribute `_cacheable` and method `_should_cache` have been removed (:issue:`23118`)
907+
- :meth:`Index.hasnans` now always returns a python boolean. Previously, it could return a python or a numpy boolean, depending on circumstances (:issue:`23294`).
907908

908909
.. _whatsnew_0240.deprecations:
909910

pandas/core/arrays/datetimelike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def _isnan(self):
219219
@property # NB: override with cache_readonly in immutable subclasses
220220
def hasnans(self):
221221
""" return if I have any nans; enables various perf speedups """
222-
return self._isnan.any()
222+
return bool(self._isnan.any())
223223

224224
def _maybe_mask_results(self, result, fill_value=None, convert=None):
225225
"""

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2221,7 +2221,7 @@ def _nan_idxs(self):
22212221
def hasnans(self):
22222222
""" return if I have any nans; enables various perf speedups """
22232223
if self._can_hold_na:
2224-
return self._isnan.any()
2224+
return bool(self._isnan.any())
22252225
else:
22262226
return False
22272227

pandas/tests/indexes/common.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ def test_get_unique_index(self, indices):
417417
# and doesn't contain nans.
418418
assert idx_unique.is_unique is True
419419
try:
420-
assert not idx_unique.hasnans
420+
assert idx_unique.hasnans is False
421421
except NotImplementedError:
422422
pass
423423

@@ -916,7 +916,7 @@ def test_hasnans_isnans(self):
916916
# cases in indices doesn't include NaN
917917
expected = np.array([False] * len(idx), dtype=bool)
918918
tm.assert_numpy_array_equal(idx._isnan, expected)
919-
assert not idx.hasnans
919+
assert idx.hasnans is False
920920

921921
idx = index.copy()
922922
values = np.asarray(idx.values)
@@ -938,7 +938,7 @@ def test_hasnans_isnans(self):
938938
expected = np.array([False] * len(idx), dtype=bool)
939939
expected[1] = True
940940
tm.assert_numpy_array_equal(idx._isnan, expected)
941-
assert idx.hasnans
941+
assert idx.hasnans is True
942942

943943
def test_fillna(self):
944944
# GH 11343
@@ -978,7 +978,7 @@ def test_fillna(self):
978978
expected = np.array([False] * len(idx), dtype=bool)
979979
expected[1] = True
980980
tm.assert_numpy_array_equal(idx._isnan, expected)
981-
assert idx.hasnans
981+
assert idx.hasnans is True
982982

983983
def test_nulls(self):
984984
# this is really a smoke test for the methods

pandas/tests/indexes/datetimes/test_ops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -356,15 +356,15 @@ def test_nat(self, tz_naive_fixture):
356356
assert idx._can_hold_na
357357

358358
tm.assert_numpy_array_equal(idx._isnan, np.array([False, False]))
359-
assert not idx.hasnans
359+
assert idx.hasnans is False
360360
tm.assert_numpy_array_equal(idx._nan_idxs,
361361
np.array([], dtype=np.intp))
362362

363363
idx = pd.DatetimeIndex(['2011-01-01', 'NaT'], tz=tz)
364364
assert idx._can_hold_na
365365

366366
tm.assert_numpy_array_equal(idx._isnan, np.array([False, True]))
367-
assert idx.hasnans
367+
assert idx.hasnans is True
368368
tm.assert_numpy_array_equal(idx._nan_idxs,
369369
np.array([1], dtype=np.intp))
370370

pandas/tests/indexes/interval/test_interval.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def test_length(self, closed, breaks):
9393

9494
def test_with_nans(self, closed):
9595
index = self.create_index(closed=closed)
96-
assert not index.hasnans
96+
assert index.hasnans is False
9797

9898
result = index.isna()
9999
expected = np.repeat(False, len(index))
@@ -104,7 +104,7 @@ def test_with_nans(self, closed):
104104
tm.assert_numpy_array_equal(result, expected)
105105

106106
index = self.create_index_with_nan(closed=closed)
107-
assert index.hasnans
107+
assert index.hasnans is True
108108

109109
result = index.isna()
110110
expected = np.array([False, True] + [False] * (len(index) - 2))

pandas/tests/indexes/multi/test_missing.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def test_fillna(idx):
4949
expected = np.array([False] * len(idx), dtype=bool)
5050
expected[1] = True
5151
tm.assert_numpy_array_equal(idx._isnan, expected)
52-
assert idx.hasnans
52+
assert idx.hasnans is True
5353

5454

5555
def test_dropna():
@@ -91,7 +91,7 @@ def test_hasnans_isnans(idx):
9191
# cases in indices doesn't include NaN
9292
expected = np.array([False] * len(index), dtype=bool)
9393
tm.assert_numpy_array_equal(index._isnan, expected)
94-
assert not index.hasnans
94+
assert index.hasnans is False
9595

9696
index = idx.copy()
9797
values = index.values
@@ -102,7 +102,7 @@ def test_hasnans_isnans(idx):
102102
expected = np.array([False] * len(index), dtype=bool)
103103
expected[1] = True
104104
tm.assert_numpy_array_equal(index._isnan, expected)
105-
assert index.hasnans
105+
assert index.hasnans is True
106106

107107

108108
def test_nan_stays_float():

pandas/tests/indexes/period/test_ops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -356,15 +356,15 @@ def test_nat(self):
356356
assert idx._can_hold_na
357357

358358
tm.assert_numpy_array_equal(idx._isnan, np.array([False, False]))
359-
assert not idx.hasnans
359+
assert idx.hasnans is False
360360
tm.assert_numpy_array_equal(idx._nan_idxs,
361361
np.array([], dtype=np.intp))
362362

363363
idx = pd.PeriodIndex(['2011-01-01', 'NaT'], freq='D')
364364
assert idx._can_hold_na
365365

366366
tm.assert_numpy_array_equal(idx._isnan, np.array([False, True]))
367-
assert idx.hasnans
367+
assert idx.hasnans is True
368368
tm.assert_numpy_array_equal(idx._nan_idxs,
369369
np.array([1], dtype=np.intp))
370370

pandas/tests/indexes/timedeltas/test_ops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,15 @@ def test_nat(self):
273273
assert idx._can_hold_na
274274

275275
tm.assert_numpy_array_equal(idx._isnan, np.array([False, False]))
276-
assert not idx.hasnans
276+
assert idx.hasnans is False
277277
tm.assert_numpy_array_equal(idx._nan_idxs,
278278
np.array([], dtype=np.intp))
279279

280280
idx = pd.TimedeltaIndex(['1 days', 'NaT'])
281281
assert idx._can_hold_na
282282

283283
tm.assert_numpy_array_equal(idx._isnan, np.array([False, True]))
284-
assert idx.hasnans
284+
assert idx.hasnans is True
285285
tm.assert_numpy_array_equal(idx._nan_idxs,
286286
np.array([1], dtype=np.intp))
287287

pandas/tests/series/test_internals.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,11 @@ def test_convert_preserve_all_bool(self):
315315
def test_hasnans_unchached_for_series():
316316
# GH#19700
317317
idx = pd.Index([0, 1])
318-
assert not idx.hasnans
318+
assert idx.hasnans is False
319319
assert 'hasnans' in idx._cache
320320
ser = idx.to_series()
321-
assert not ser.hasnans
321+
assert ser.hasnans is False
322322
assert not hasattr(ser, '_cache')
323323
ser.iloc[-1] = np.nan
324-
assert ser.hasnans
324+
assert ser.hasnans is True
325325
assert pd.Series.hasnans.__doc__ == pd.Index.hasnans.__doc__

0 commit comments

Comments
 (0)