Skip to content

Commit fbcd5ab

Browse files
committed
BUG: make sure that nan/none like values to Timestamp are returned as NaT
1 parent 7e4ccbe commit fbcd5ab

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

pandas/tseries/tests/test_timeseries.py

+34-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import pandas.util.py3compat as py3compat
3939
from pandas.core.datetools import BDay
4040
import pandas.core.common as com
41+
from pandas import concat
4142

4243
from numpy.testing.decorators import slow
4344

@@ -171,7 +172,6 @@ def test_indexing_over_size_cutoff(self):
171172
def test_indexing_unordered(self):
172173

173174
# GH 2437
174-
from pandas import concat
175175
rng = date_range(start='2011-01-01', end='2011-01-15')
176176
ts = Series(randn(len(rng)), index=rng)
177177
ts2 = concat([ts[0:4],ts[-4:],ts[4:-4]])
@@ -601,6 +601,26 @@ def test_to_datetime_unit(self):
601601
expected = Series([ Timestamp('2013-06-09 02:42:28') + timedelta(seconds=t) for t in range(20) ])
602602
assert_series_equal(result,expected)
603603

604+
s = Series([ epoch + t for t in range(20) ]).astype(float)
605+
result = to_datetime(s,unit='s')
606+
expected = Series([ Timestamp('2013-06-09 02:42:28') + timedelta(seconds=t) for t in range(20) ])
607+
assert_series_equal(result,expected)
608+
609+
s = Series([ epoch + t for t in range(20) ] + [iNaT])
610+
result = to_datetime(s,unit='s')
611+
expected = Series([ Timestamp('2013-06-09 02:42:28') + timedelta(seconds=t) for t in range(20) ] + [NaT])
612+
assert_series_equal(result,expected)
613+
614+
s = Series([ epoch + t for t in range(20) ] + [iNaT]).astype(float)
615+
result = to_datetime(s,unit='s')
616+
expected = Series([ Timestamp('2013-06-09 02:42:28') + timedelta(seconds=t) for t in range(20) ] + [NaT])
617+
assert_series_equal(result,expected)
618+
619+
s = concat([Series([ epoch + t for t in range(20) ]).astype(float),Series([np.nan])],ignore_index=True)
620+
result = to_datetime(s,unit='s')
621+
expected = Series([ Timestamp('2013-06-09 02:42:28') + timedelta(seconds=t) for t in range(20) ] + [NaT])
622+
assert_series_equal(result,expected)
623+
604624
def test_series_ctor_datetime64(self):
605625
rng = date_range('1/1/2000 00:00:00', '1/1/2000 1:59:50',
606626
freq='10s')
@@ -2741,6 +2761,19 @@ def check(val,unit=None,s=1,us=0):
27412761
check(val/1000000.0 + 0.005,unit='ms',us=5)
27422762
check(val/1000000000.0 + 0.5,unit='s',us=500000)
27432763

2764+
# nan
2765+
result = Timestamp(np.nan)
2766+
self.assert_(result is NaT)
2767+
2768+
result = Timestamp(None)
2769+
self.assert_(result is NaT)
2770+
2771+
result = Timestamp(iNaT)
2772+
self.assert_(result is NaT)
2773+
2774+
result = Timestamp(NaT)
2775+
self.assert_(result is NaT)
2776+
27442777
def test_comparison(self):
27452778
# 5-18-2012 00:00:00.000
27462779
stamp = 1337299200000000000L

pandas/tslib.pyx

+23-9
Original file line numberDiff line numberDiff line change
@@ -640,17 +640,25 @@ cdef convert_to_tsobject(object ts, object tz, object unit):
640640

641641
obj = _TSObject()
642642

643-
if is_datetime64_object(ts):
643+
if ts is None or ts is NaT:
644+
obj.value = NPY_NAT
645+
elif is_datetime64_object(ts):
644646
obj.value = _get_datetime64_nanos(ts)
645647
pandas_datetime_to_datetimestruct(obj.value, PANDAS_FR_ns, &obj.dts)
646648
elif is_integer_object(ts):
647-
ts = ts * cast_from_unit(unit,None)
648-
obj.value = ts
649-
pandas_datetime_to_datetimestruct(ts, PANDAS_FR_ns, &obj.dts)
649+
if ts == NPY_NAT:
650+
obj.value = NPY_NAT
651+
else:
652+
ts = ts * cast_from_unit(unit,None)
653+
obj.value = ts
654+
pandas_datetime_to_datetimestruct(ts, PANDAS_FR_ns, &obj.dts)
650655
elif util.is_float_object(ts):
651-
ts = cast_from_unit(unit,ts)
652-
obj.value = ts
653-
pandas_datetime_to_datetimestruct(ts, PANDAS_FR_ns, &obj.dts)
656+
if ts != ts or ts == NPY_NAT:
657+
obj.value = NPY_NAT
658+
else:
659+
ts = cast_from_unit(unit,ts)
660+
obj.value = ts
661+
pandas_datetime_to_datetimestruct(ts, PANDAS_FR_ns, &obj.dts)
654662
elif util.is_string_object(ts):
655663
if ts in _nat_strings:
656664
obj.value = NPY_NAT
@@ -864,9 +872,15 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
864872

865873
# if we are coercing, dont' allow integers
866874
elif util.is_integer_object(val) and not coerce:
867-
iresult[i] = val*m
875+
if val == iNaT:
876+
iresult[i] = iNaT
877+
else:
878+
iresult[i] = val*m
868879
elif util.is_float_object(val) and not coerce:
869-
iresult[i] = cast_from_unit(unit,val)
880+
if val != val or val == iNaT:
881+
iresult[i] = iNaT
882+
else:
883+
iresult[i] = cast_from_unit(unit,val)
870884
else:
871885
try:
872886
if len(val) == 0:

0 commit comments

Comments
 (0)