Skip to content

BUG: isnull doesnt handle PeriodNaT properly #7557

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

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 3 additions & 2 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ def _isnull_new(obj):
return obj._constructor(obj._data.isnull(func=isnull))
elif isinstance(obj, list) or hasattr(obj, '__array__'):
return _isnull_ndarraylike(np.asarray(obj))
elif isinstance(obj, pd.Period):
return obj.ordinal == tslib.iNaT
else:
return obj is None

Expand Down Expand Up @@ -291,7 +293,6 @@ def _isnull_ndarraylike(obj):
values = values.values
result = values.isnull()
else:

# Working around NumPy ticket 1542
shape = values.shape

Expand All @@ -302,7 +303,7 @@ def _isnull_ndarraylike(obj):
vec = lib.isnullobj(values.ravel())
result[...] = vec.reshape(shape)

elif is_datetimelike(obj):
elif dtype in _DATELIKE_DTYPES or isinstance(obj, pd.PeriodIndex):
# this is the NaT pattern
result = values.view('i8') == tslib.iNaT
else:
Expand Down
6 changes: 5 additions & 1 deletion pandas/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ cpdef checknull(object val):
return get_timedelta64_value(val) == NPY_NAT
elif is_array(val):
return False
# elif is_periodnat(val):
# return True
else:
return _checknull(val)

Expand Down Expand Up @@ -278,6 +280,8 @@ def item_from_zerodim(object val):
"""
return util.unbox_if_zerodim(val)

cdef is_periodnat(object val):
return hasattr(val, 'freq') and getattr(val, 'ordinal', None) == iNaT

@cython.wraparound(False)
@cython.boundscheck(False)
Expand All @@ -290,7 +294,7 @@ def isnullobj(ndarray[object] arr):
result = np.zeros(n, dtype=np.uint8)
for i from 0 <= i < n:
val = arr[i]
result[i] = val is NaT or _checknull(val)
result[i] = val is NaT or _checknull(val) or is_periodnat(val)
return result.view(np.bool_)

@cython.wraparound(False)
Expand Down
37 changes: 37 additions & 0 deletions pandas/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import nose
from nose.tools import assert_equal
import numpy as np
import pandas as pd
from pandas.tslib import iNaT, NaT
from pandas import Series, DataFrame, date_range, DatetimeIndex, Timestamp, Float64Index
from pandas import compat
Expand Down Expand Up @@ -143,6 +144,18 @@ def test_isnull_nat():
exp = np.array([True])
assert(np.array_equal(result, exp))

result = isnull(pd.Period('NaT', freq='M'))
assert(result)
result = isnull([pd.Period('NaT', freq='M')])
exp = np.array([True])
assert(np.array_equal(result, exp))

result = notnull(pd.Period('NaT', freq='M'))
assert(not result)
result = notnull([pd.Period('NaT', freq='M')])
exp = np.array([False])
assert(np.array_equal(result, exp))

def test_isnull_datetime():
assert (not isnull(datetime.now()))
assert notnull(datetime.now())
Expand All @@ -166,6 +179,30 @@ def test_isnull_datetime():
mask = isnull(pidx[1:])
assert(not mask.any())

def test_isnull_period():
assert (not isnull(pd.Period('2011-01', freq='M')))
assert notnull(pd.Period('2011-01', freq='M'))

idx = pd.period_range('1/1/1990', periods=20)
assert(notnull(idx).all())

idx = pd.PeriodIndex(['NaT', '2011-01', '2011-02'], freq='M')
mask = isnull(idx)
assert(mask[0])
assert(not mask[1:].any())

mask = isnull(idx.asobject)
assert(mask[0])
assert(not mask[1:].any())

mask = notnull(idx)
assert(not mask[0])
assert(mask[1:].all())

mask = notnull(idx.asobject)
assert(not mask[0])
assert(mask[1:].all())


class TestIsNull(tm.TestCase):
def test_0d_array(self):
Expand Down
4 changes: 2 additions & 2 deletions vb_suite/panel_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
# shift

setup = common_setup + """
index = date_range(start="2000", freq="D", periods=1000)
panel = Panel(np.random.randn(100, len(index), 1000))
index = date_range(start="2000", freq="D", periods=100)
panel = Panel(np.random.randn(100, len(index), 100))
"""

panel_shift = Benchmark('panel.shift(1)', setup,
Expand Down