Skip to content

Commit f216c74

Browse files
committed
BUG/TST: Fixes isnull behavior on NaT in array. Closes #5443
common._isnull_ndarraylike(...) uses lib.isnullobj to check nulls/NaN/NaT in ndarray, which in turn relies on util._checknull. _checknull did not know about NaT, but now lib.isnullobj does, while still maintaining performance by doing arr[i] only once. Added a test case test_isnull_nat() to test_common.py and check for NaT in lib.isnullobj. pd.isnull(np.array([pd.NaT])) now yields the correct results ([True]).
1 parent 3737372 commit f216c74

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

pandas/lib.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ cdef inline int64_t get_timedelta64_value(val):
170170
cdef double INF = <double> np.inf
171171
cdef double NEGINF = -INF
172172

173-
174173
cpdef checknull(object val):
175174
if util.is_float_object(val) or util.is_complex_object(val):
176175
return val != val # and val != INF and val != NEGINF
@@ -183,7 +182,7 @@ cpdef checknull(object val):
183182
elif is_array(val):
184183
return False
185184
else:
186-
return util._checknull(val)
185+
return _checknull(val)
187186

188187
cpdef checknull_old(object val):
189188
if util.is_float_object(val) or util.is_complex_object(val):
@@ -213,7 +212,8 @@ def isnullobj(ndarray[object] arr):
213212
n = len(arr)
214213
result = np.zeros(n, dtype=np.uint8)
215214
for i from 0 <= i < n:
216-
result[i] = util._checknull(arr[i])
215+
arobj = arr[i]
216+
result[i] = arobj is NaT or _checknull(arobj)
217217
return result.view(np.bool_)
218218

219219
@cython.wraparound(False)

pandas/src/util.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ cdef inline is_array(object o):
6767

6868
cdef inline bint _checknull(object val):
6969
try:
70-
return val is None or (cpython.PyFloat_Check(val) and val != val)
70+
return val is None or (cpython.PyFloat_Check(val) and val != val)
7171
except ValueError:
7272
return False
7373

pandas/tests/test_common.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import nose
66
from nose.tools import assert_equal
77
import numpy as np
8-
from pandas.tslib import iNaT
8+
from pandas.tslib import iNaT, NaT
99

1010
from pandas import Series, DataFrame, date_range, DatetimeIndex, Timestamp
1111
from pandas import compat
@@ -114,6 +114,14 @@ def test_isnull_lists():
114114
result = isnull([u('foo'), u('bar')])
115115
assert(not result.any())
116116

117+
def test_isnull_nat():
118+
result = isnull([NaT])
119+
exp = np.array([True])
120+
assert(np.array_equal(result, exp))
121+
122+
result = isnull(np.array([NaT], dtype=object))
123+
exp = np.array([True])
124+
assert(np.array_equal(result, exp))
117125

118126
def test_isnull_datetime():
119127
assert (not isnull(datetime.now()))

0 commit comments

Comments
 (0)