Skip to content

Commit

Permalink
fix Series.hasnans to not cache pandas-dev#19700
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel committed Jul 11, 2018
1 parent 5d0daa0 commit e713091
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
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 @@ -369,6 +369,7 @@ Missing
^^^^^^^

- Bug in :func:`DataFrame.fillna` where a ``ValueError`` would raise when one column contained a ``datetime64[ns, tz]`` dtype (:issue:`15522`)
- 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`)

MultiIndex
^^^^^^^^^^
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
['asobject', 'sortlevel', 'reshape', 'get_value', 'set_value',
'from_csv', 'valid'])

# Override cache_readonly bc Series is mutable
hasnans = property(base.IndexOpsMixin.hasnans.func,
doc=base.IndexOpsMixin.hasnans.__doc__)

def __init__(self, data=None, index=None, dtype=None, name=None,
copy=False, fastpath=False):

Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/series/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pandas import Series
from pandas.core.indexes.datetimes import Timestamp
import pandas._libs.lib as lib
import pandas as pd

from pandas.util.testing import assert_series_equal
import pandas.util.testing as tm
Expand Down Expand Up @@ -309,3 +310,16 @@ def test_convert_preserve_all_bool(self):
r = s._convert(datetime=True, numeric=True)
e = Series([False, True, False, False], dtype=bool)
tm.assert_series_equal(r, e)


def test_hasnans_unchached_for_series():
# GH#19700
idx = pd.Index([0, 1])
assert not idx.hasnans
assert 'hasnans' in idx._cache
ser = idx.to_series()
assert not ser.hasnans
assert not hasattr(ser, '_cache')
ser.iloc[-1] = np.nan
assert ser.hasnans
assert pd.Series.hasnans.__doc__ == pd.Index.hasnans.__doc__

0 comments on commit e713091

Please sign in to comment.