Skip to content

Commit e713091

Browse files
committed
fix Series.hasnans to not cache pandas-dev#19700
1 parent 5d0daa0 commit e713091

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ Missing
369369
^^^^^^^
370370

371371
- Bug in :func:`DataFrame.fillna` where a ``ValueError`` would raise when one column contained a ``datetime64[ns, tz]`` dtype (:issue:`15522`)
372+
- Bug in :func:`Series.hasnans` that could be incorrectly cached and return incorrect answers if null elements are introduced after an initial call (:issue:`19700`)
372373

373374
MultiIndex
374375
^^^^^^^^^^

pandas/core/series.py

+4
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
166166
['asobject', 'sortlevel', 'reshape', 'get_value', 'set_value',
167167
'from_csv', 'valid'])
168168

169+
# Override cache_readonly bc Series is mutable
170+
hasnans = property(base.IndexOpsMixin.hasnans.func,
171+
doc=base.IndexOpsMixin.hasnans.__doc__)
172+
169173
def __init__(self, data=None, index=None, dtype=None, name=None,
170174
copy=False, fastpath=False):
171175

pandas/tests/series/test_internals.py

+14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pandas import Series
1212
from pandas.core.indexes.datetimes import Timestamp
1313
import pandas._libs.lib as lib
14+
import pandas as pd
1415

1516
from pandas.util.testing import assert_series_equal
1617
import pandas.util.testing as tm
@@ -309,3 +310,16 @@ def test_convert_preserve_all_bool(self):
309310
r = s._convert(datetime=True, numeric=True)
310311
e = Series([False, True, False, False], dtype=bool)
311312
tm.assert_series_equal(r, e)
313+
314+
315+
def test_hasnans_unchached_for_series():
316+
# GH#19700
317+
idx = pd.Index([0, 1])
318+
assert not idx.hasnans
319+
assert 'hasnans' in idx._cache
320+
ser = idx.to_series()
321+
assert not ser.hasnans
322+
assert not hasattr(ser, '_cache')
323+
ser.iloc[-1] = np.nan
324+
assert ser.hasnans
325+
assert pd.Series.hasnans.__doc__ == pd.Index.hasnans.__doc__

0 commit comments

Comments
 (0)