Skip to content

Commit 9c68809

Browse files
Mike Kellyjreback
Mike Kelly
authored andcommitted
GH3969 Implement unit='D' in to_datetime
1 parent f5a2b35 commit 9c68809

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

pandas/tseries/tests/test_timeseries.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -2752,23 +2752,31 @@ def test_basics_nanos(self):
27522752
self.assert_(stamp.nanosecond == 500)
27532753

27542754
def test_unit(self):
2755-
def check(val,unit=None,s=1,us=0):
2755+
def check(val,unit=None,h=1,s=1,us=0):
27562756
stamp = Timestamp(val, unit=unit)
27572757
self.assert_(stamp.year == 2000)
27582758
self.assert_(stamp.month == 1)
27592759
self.assert_(stamp.day == 1)
2760-
self.assert_(stamp.hour == 1)
2761-
self.assert_(stamp.minute == 1)
2762-
self.assert_(stamp.second == s)
2763-
self.assert_(stamp.microsecond == us)
2760+
self.assert_(stamp.hour == h)
2761+
if unit != 'D':
2762+
self.assert_(stamp.minute == 1)
2763+
self.assert_(stamp.second == s)
2764+
self.assert_(stamp.microsecond == us)
2765+
else:
2766+
self.assert_(stamp.minute == 0)
2767+
self.assert_(stamp.second == 0)
2768+
self.assert_(stamp.microsecond == 0)
27642769
self.assert_(stamp.nanosecond == 0)
27652770

2766-
val = Timestamp('20000101 01:01:01').value
2771+
ts = Timestamp('20000101 01:01:01')
2772+
val = ts.value
2773+
days = (ts - Timestamp('1970-01-01')).days
27672774

27682775
check(val)
27692776
check(val/1000L,unit='us')
27702777
check(val/1000000L,unit='ms')
27712778
check(val/1000000000L,unit='s')
2779+
check(days,unit='D',h=0)
27722780

27732781
# using truediv, so these are like floats
27742782
if py3compat.PY3:
@@ -2792,6 +2800,7 @@ def check(val,unit=None,s=1,us=0):
27922800
check(val/1000000.0 + 0.5,unit='ms',us=500)
27932801
check(val/1000000.0 + 0.005,unit='ms',us=5)
27942802
check(val/1000000000.0 + 0.5,unit='s',us=500000)
2803+
check(days + 0.5,unit='D',h=12)
27952804

27962805
# nan
27972806
result = Timestamp(np.nan)

pandas/tseries/tools.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def to_datetime(arg, errors='ignore', dayfirst=False, utc=None, box=True,
6969
format : string, default None
7070
strftime to parse time, eg "%d/%m/%Y"
7171
coerce : force errors to NaT (False by default)
72-
unit : unit of the arg (s,ms,us,ns) denote the unit in epoch
72+
unit : unit of the arg (D,s,ms,us,ns) denote the unit in epoch
7373
(e.g. a unix timestamp), which is an integer/float number
7474
7575
Returns

pandas/tslib.pyx

+4-1
Original file line numberDiff line numberDiff line change
@@ -1269,7 +1269,10 @@ cdef inline _get_datetime64_nanos(object val):
12691269
cdef inline int64_t cast_from_unit(object unit, object ts):
12701270
""" return a casting of the unit represented to nanoseconds
12711271
round the fractional part of a float to our precision, p """
1272-
if unit == 's':
1272+
if unit == 'D':
1273+
m = 1000000000L * 86400
1274+
p = 6
1275+
elif unit == 's':
12731276
m = 1000000000L
12741277
p = 6
12751278
elif unit == 'ms':

0 commit comments

Comments
 (0)