Skip to content

Commit 47409a0

Browse files
committed
COMPAT: fix array_to_datetime on comparison of NaT
1 parent 59a38d4 commit 47409a0

File tree

6 files changed

+48
-23
lines changed

6 files changed

+48
-23
lines changed

pandas/core/common.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,17 @@ def notnull(obj):
285285
def _is_null_datelike_scalar(other):
286286
""" test whether the object is a null datelike, e.g. Nat
287287
but guard against passing a non-scalar """
288-
return (np.isscalar(other) and (isnull(other) or other == tslib.iNaT)) or other is pd.NaT or other is None
288+
if other is pd.NaT or other is None:
289+
return True
290+
elif np.isscalar(other):
291+
292+
# a timedelta
293+
if hasattr(other,'dtype'):
294+
return other.view('i8') == tslib.iNaT
295+
elif is_integer(other) and other == tslib.iNaT:
296+
return True
297+
return isnull(other)
298+
return False
289299

290300
def array_equivalent(left, right):
291301
"""

pandas/core/format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1861,7 +1861,7 @@ def _get_format_timedelta64(values):
18611861
def impl(x):
18621862
if x is None or lib.checknull(x):
18631863
return 'NaT'
1864-
elif format_short and x == 0:
1864+
elif format_short and com.is_integer(x) and x.view('int64') == 0:
18651865
return "0 days" if even_days else "00:00:00"
18661866
else:
18671867
return lib.repr_timedelta64(x, format=format)

pandas/src/inference.pyx

+12-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,18 @@ def infer_dtype_list(list values):
152152

153153

154154
cdef inline bint is_null_datetimelike(v):
155-
return util._checknull(v) or (util.is_integer_object(v) and v == iNaT) or v is NaT
155+
# determine if we have a null for a timedelta/datetime (or integer versions)x
156+
if util._checknull(v):
157+
return True
158+
elif util.is_timedelta64_object(v):
159+
return v.view('int64') == iNaT
160+
elif util.is_datetime64_object(v):
161+
return v.view('int64') == iNaT
162+
elif util.is_integer_object(v):
163+
return v == iNaT
164+
elif v is NaT:
165+
return True
166+
return False
156167

157168
cdef inline bint is_datetime(object o):
158169
return PyDateTime_Check(o)

pandas/tseries/tests/test_timedeltas.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ def conv(v):
122122
def test_nat_converters(self):
123123
_skip_if_numpy_not_friendly()
124124

125-
self.assertEqual(to_timedelta('nat',box=False), tslib.iNaT)
126-
self.assertEqual(to_timedelta('nan',box=False), tslib.iNaT)
125+
self.assertEqual(to_timedelta('nat',box=False).astype('int64'), tslib.iNaT)
126+
self.assertEqual(to_timedelta('nan',box=False).astype('int64'), tslib.iNaT)
127127

128128
def test_to_timedelta(self):
129129
_skip_if_numpy_not_friendly()
@@ -137,7 +137,7 @@ def conv(v):
137137

138138
# empty string
139139
result = to_timedelta('',box=False)
140-
self.assertEqual(result, tslib.iNaT)
140+
self.assertEqual(result.astype('int64'), tslib.iNaT)
141141

142142
result = to_timedelta(['', ''])
143143
self.assert_(isnull(result).all())
@@ -302,10 +302,10 @@ def test_to_timedelta_on_missing_values(self):
302302
assert_series_equal(actual, expected)
303303

304304
actual = pd.to_timedelta(np.nan)
305-
self.assertEqual(actual, timedelta_NaT)
305+
self.assertEqual(actual.astype('int64'), timedelta_NaT.astype('int64'))
306306

307307
actual = pd.to_timedelta(pd.NaT)
308-
self.assertEqual(actual, timedelta_NaT)
308+
self.assertEqual(actual.astype('int64'), timedelta_NaT.astype('int64'))
309309

310310
def test_timedelta_ops_with_missing_values(self):
311311
_skip_if_numpy_not_friendly()

pandas/tseries/util.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def pivot_annual(series, freq=None):
5252
offset = index.dayofyear - 1
5353

5454
# adjust for leap year
55-
offset[(-isleapyear(year)) & (offset >= 59)] += 1
55+
offset[(~isleapyear(year)) & (offset >= 59)] += 1
5656

5757
columns = lrange(1, 367)
5858
# todo: strings like 1/1, 1/25, etc.?
@@ -66,7 +66,7 @@ def pivot_annual(series, freq=None):
6666
defaulted = grouped.apply(lambda x: x.reset_index(drop=True))
6767
defaulted.index = defaulted.index.droplevel(0)
6868
offset = np.asarray(defaulted.index)
69-
offset[-isleapyear(year) & (offset >= 1416)] += 24
69+
offset[~isleapyear(year) & (offset >= 1416)] += 24
7070
columns = lrange(1, 8785)
7171
else:
7272
raise NotImplementedError(freq)

pandas/tslib.pyx

+17-13
Original file line numberDiff line numberDiff line change
@@ -402,17 +402,17 @@ class Timestamp(_Timestamp):
402402
if month <= 2:
403403
year -= 1
404404
month += 12
405-
return (day +
406-
np.fix((153*month - 457)/5) +
407-
365*year +
408-
np.floor(year / 4) -
409-
np.floor(year / 100) +
410-
np.floor(year / 400) +
411-
1721118.5 +
412-
(self.hour +
413-
self.minute/60.0 +
414-
self.second/3600.0 +
415-
self.microsecond/3600.0/1e+6 +
405+
return (day +
406+
np.fix((153*month - 457)/5) +
407+
365*year +
408+
np.floor(year / 4) -
409+
np.floor(year / 100) +
410+
np.floor(year / 400) +
411+
1721118.5 +
412+
(self.hour +
413+
self.minute/60.0 +
414+
self.second/3600.0 +
415+
self.microsecond/3600.0/1e+6 +
416416
self.nanosecond/3600.0/1e+9
417417
)/24.0)
418418

@@ -1114,7 +1114,7 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
11141114
continue
11151115
raise
11161116
elif util.is_datetime64_object(val):
1117-
if val == np_NaT:
1117+
if val is np_NaT or val.view('i8') == iNaT:
11181118
iresult[i] = iNaT
11191119
else:
11201120
try:
@@ -1190,7 +1190,11 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
11901190
oresult = np.empty(n, dtype=object)
11911191
for i in range(n):
11921192
val = values[i]
1193-
if util.is_datetime64_object(val):
1193+
1194+
# set as nan if is even a datetime NaT
1195+
if _checknull_with_nat(val) or val is np_NaT:
1196+
oresult[i] = np.nan
1197+
elif util.is_datetime64_object(val):
11941198
oresult[i] = val.item()
11951199
else:
11961200
oresult[i] = val

0 commit comments

Comments
 (0)