Skip to content

API/TST: make hasnans always return python booleans #23349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,7 @@ Other API Changes
has an improved ``KeyError`` message, and will not fail on duplicate column names with ``drop=True``. (:issue:`22484`)
- 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`)
- :class:`DateOffset` attribute `_cacheable` and method `_should_cache` have been removed (:issue:`23118`)
- :meth:`Index.hasnans` and :meth:`Series.hasnans` now always return a python boolean. Previously, a python or a numpy boolean could be returned, depending on circumstances (:issue:`23294`).

.. _whatsnew_0240.deprecations:

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def _isnan(self):
@property # NB: override with cache_readonly in immutable subclasses
def hasnans(self):
""" return if I have any nans; enables various perf speedups """
return self._isnan.any()
return bool(self._isnan.any())

def _maybe_mask_results(self, result, fill_value=None, convert=None):
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ def __iter__(self):
@cache_readonly
def hasnans(self):
""" return if I have any nans; enables various perf speedups """
return isna(self).any()
return bool(isna(self).any())

def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
filter_type=None, **kwds):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2221,7 +2221,7 @@ def _nan_idxs(self):
def hasnans(self):
""" return if I have any nans; enables various perf speedups """
if self._can_hold_na:
return self._isnan.any()
return bool(self._isnan.any())
else:
return False

Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ def test_get_unique_index(self, indices):
# and doesn't contain nans.
assert idx_unique.is_unique is True
try:
assert not idx_unique.hasnans
assert idx_unique.hasnans is False
except NotImplementedError:
pass

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

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

def test_fillna(self):
# GH 11343
Expand Down Expand Up @@ -978,7 +978,7 @@ def test_fillna(self):
expected = np.array([False] * len(idx), dtype=bool)
expected[1] = True
tm.assert_numpy_array_equal(idx._isnan, expected)
assert idx.hasnans
assert idx.hasnans is True

def test_nulls(self):
# this is really a smoke test for the methods
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/datetimes/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,15 +356,15 @@ def test_nat(self, tz_naive_fixture):
assert idx._can_hold_na

tm.assert_numpy_array_equal(idx._isnan, np.array([False, False]))
assert not idx.hasnans
assert idx.hasnans is False
tm.assert_numpy_array_equal(idx._nan_idxs,
np.array([], dtype=np.intp))

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

tm.assert_numpy_array_equal(idx._isnan, np.array([False, True]))
assert idx.hasnans
assert idx.hasnans is True
tm.assert_numpy_array_equal(idx._nan_idxs,
np.array([1], dtype=np.intp))

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_length(self, closed, breaks):

def test_with_nans(self, closed):
index = self.create_index(closed=closed)
assert not index.hasnans
assert index.hasnans is False

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

index = self.create_index_with_nan(closed=closed)
assert index.hasnans
assert index.hasnans is True

result = index.isna()
expected = np.array([False, True] + [False] * (len(index) - 2))
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/indexes/multi/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_fillna(idx):
expected = np.array([False] * len(idx), dtype=bool)
expected[1] = True
tm.assert_numpy_array_equal(idx._isnan, expected)
assert idx.hasnans
assert idx.hasnans is True


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

index = idx.copy()
values = index.values
Expand All @@ -102,7 +102,7 @@ def test_hasnans_isnans(idx):
expected = np.array([False] * len(index), dtype=bool)
expected[1] = True
tm.assert_numpy_array_equal(index._isnan, expected)
assert index.hasnans
assert index.hasnans is True


def test_nan_stays_float():
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/period/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,15 +356,15 @@ def test_nat(self):
assert idx._can_hold_na

tm.assert_numpy_array_equal(idx._isnan, np.array([False, False]))
assert not idx.hasnans
assert idx.hasnans is False
tm.assert_numpy_array_equal(idx._nan_idxs,
np.array([], dtype=np.intp))

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

tm.assert_numpy_array_equal(idx._isnan, np.array([False, True]))
assert idx.hasnans
assert idx.hasnans is True
tm.assert_numpy_array_equal(idx._nan_idxs,
np.array([1], dtype=np.intp))

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/timedeltas/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,15 @@ def test_nat(self):
assert idx._can_hold_na

tm.assert_numpy_array_equal(idx._isnan, np.array([False, False]))
assert not idx.hasnans
assert idx.hasnans is False
tm.assert_numpy_array_equal(idx._nan_idxs,
np.array([], dtype=np.intp))

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

tm.assert_numpy_array_equal(idx._isnan, np.array([False, True]))
assert idx.hasnans
assert idx.hasnans is True
tm.assert_numpy_array_equal(idx._nan_idxs,
np.array([1], dtype=np.intp))

Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/series/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,11 @@ def test_convert_preserve_all_bool(self):
def test_hasnans_unchached_for_series():
# GH#19700
idx = pd.Index([0, 1])
assert not idx.hasnans
assert idx.hasnans is False
assert 'hasnans' in idx._cache
ser = idx.to_series()
assert not ser.hasnans
assert ser.hasnans is False
assert not hasattr(ser, '_cache')
ser.iloc[-1] = np.nan
assert ser.hasnans
assert ser.hasnans is True
assert pd.Series.hasnans.__doc__ == pd.Index.hasnans.__doc__