Skip to content

Commit 7e4ccbe

Browse files
committed
TST: disallow slicing a timeseries with floats
TST: manage truediv in py3 for unit comparisons
1 parent 73d58a9 commit 7e4ccbe

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

pandas/tseries/index.py

+3
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,9 @@ def slice_indexer(self, start=None, end=None, step=None):
12041204
if isinstance(start, time) or isinstance(end, time):
12051205
raise KeyError('Cannot mix time and non-time slice keys')
12061206

1207+
if isinstance(start, float) or isinstance(end, float):
1208+
raise TypeError('Cannot index datetime64 with float keys')
1209+
12071210
return Index.slice_indexer(self, start, end, step)
12081211

12091212
def slice_locs(self, start=None, end=None):

pandas/tseries/tests/test_timeseries.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -2718,10 +2718,17 @@ def check(val,unit=None,s=1,us=0):
27182718
check(val/1000000L,unit='ms')
27192719
check(val/1000000000L,unit='s')
27202720

2721-
# get chopped
2722-
check((val+500000)/1000000000L,unit='s')
2723-
check((val+500000000)/1000000000L,unit='s')
2724-
check((val+500000)/1000000L,unit='ms')
2721+
# using truediv, so these are like floats
2722+
if py3compat.PY3:
2723+
check((val+500000)/1000000000L,unit='s',us=500)
2724+
check((val+500000000)/1000000000L,unit='s',us=500000)
2725+
check((val+500000)/1000000L,unit='ms',us=500)
2726+
2727+
# get chopped in py2
2728+
else:
2729+
check((val+500000)/1000000000L,unit='s')
2730+
check((val+500000000)/1000000000L,unit='s')
2731+
check((val+500000)/1000000L,unit='ms')
27252732

27262733
# ok
27272734
check((val+500000)/1000L,unit='us',us=500)

pandas/tseries/tools.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def _maybe_get_tz(tz):
5050

5151

5252
def to_datetime(arg, errors='ignore', dayfirst=False, utc=None, box=True,
53-
format=None, coerce=False, unit=None):
53+
format=None, coerce=False, unit='ns'):
5454
"""
5555
Convert argument to datetime
5656
@@ -70,7 +70,7 @@ def to_datetime(arg, errors='ignore', dayfirst=False, utc=None, box=True,
7070
strftime to parse time, eg "%d/%m/%Y"
7171
coerce : force errors to NaT (False by default)
7272
unit : unit of the arg (s,ms,us,ns) denote the unit in epoch
73-
(e.g. a unix timestamp)
73+
(e.g. a unix timestamp), which is an integer/float number
7474
7575
Returns
7676
-------

pandas/tslib.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,6 @@ cdef inline _get_datetime64_nanos(object val):
12541254
cdef inline int64_t cast_from_unit(object unit, object ts):
12551255
""" return a casting of the unit represented to nanoseconds
12561256
round the fractional part of a float to our precision, p """
1257-
p = 0
12581257
if unit == 's':
12591258
m = 1000000000L
12601259
p = 6
@@ -1266,6 +1265,7 @@ cdef inline int64_t cast_from_unit(object unit, object ts):
12661265
p = 0
12671266
else:
12681267
m = 1L
1268+
p = 0
12691269

12701270
# just give me the unit back
12711271
if ts is None:

0 commit comments

Comments
 (0)