Skip to content

Commit 737c5ce

Browse files
committed
BUG: handle list of integers in DatetimeIndex constructor, close #1303
1 parent 7567fff commit 737c5ce

File tree

5 files changed

+27
-15
lines changed

5 files changed

+27
-15
lines changed

pandas/io/pytables.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ def _read_array(group, key):
962962

963963
def _unconvert_index(data, kind):
964964
if kind == 'datetime64':
965-
index = np.array(data, dtype='M8[ns]')
965+
index = np.asarray(data, dtype='M8[ns]')
966966
elif kind == 'datetime':
967967
index = np.array([datetime.fromtimestamp(v) for v in data],
968968
dtype=object)
@@ -978,7 +978,7 @@ def _unconvert_index(data, kind):
978978

979979
def _unconvert_index_legacy(data, kind, legacy=False):
980980
if kind == 'datetime':
981-
index = lib.array_to_datetime(data)
981+
index = lib.time64_to_datetime(data)
982982
elif kind in ('string', 'integer'):
983983
index = np.array(data, dtype=object)
984984
else: # pragma: no cover

pandas/src/datetime.pyx

+7-5
Original file line numberDiff line numberDiff line change
@@ -801,9 +801,9 @@ cdef class DayOffset(_Offset):
801801
# offset.next()
802802
# return i
803803

804-
def string_to_datetime(ndarray[object] strings, raise_=False, dayfirst=False):
804+
def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False):
805805
cdef:
806-
Py_ssize_t i, n = len(strings)
806+
Py_ssize_t i, n = len(values)
807807
object val
808808
ndarray[int64_t] iresult
809809
ndarray[object] oresult
@@ -815,7 +815,7 @@ def string_to_datetime(ndarray[object] strings, raise_=False, dayfirst=False):
815815
result = np.empty(n, dtype='M8[ns]')
816816
iresult = result.view('i8')
817817
for i in range(n):
818-
val = strings[i]
818+
val = values[i]
819819
if util._checknull(val):
820820
iresult[i] = NaT
821821
elif PyDateTime_Check(val):
@@ -824,6 +824,8 @@ def string_to_datetime(ndarray[object] strings, raise_=False, dayfirst=False):
824824
iresult[i] = _date_to_datetime64(val, &dts)
825825
elif util.is_datetime64_object(val):
826826
iresult[i] = _get_datetime64_nanos(val)
827+
elif util.is_integer_object(val):
828+
iresult[i] = val
827829
else:
828830
if len(val) == 0:
829831
iresult[i] = NaT
@@ -837,7 +839,7 @@ def string_to_datetime(ndarray[object] strings, raise_=False, dayfirst=False):
837839
oresult = np.empty(n, dtype=object)
838840

839841
for i in range(n):
840-
val = strings[i]
842+
val = values[i]
841843
if util._checknull(val):
842844
oresult[i] = val
843845
else:
@@ -849,7 +851,7 @@ def string_to_datetime(ndarray[object] strings, raise_=False, dayfirst=False):
849851
except Exception:
850852
if raise_:
851853
raise
852-
return strings
854+
return values
853855
# oresult[i] = val
854856

855857
return oresult

pandas/src/tseries.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def array_to_timestamp(ndarray[object, ndim=1] arr):
162162

163163
return result
164164

165-
def array_to_datetime(ndarray[int64_t, ndim=1] arr):
165+
def time64_to_datetime(ndarray[int64_t, ndim=1] arr):
166166
cdef int i, n
167167
cdef ndarray[object, ndim=1] result
168168

pandas/tseries/tests/test_timeseries.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ def test_string_na_nat_conversion(self):
396396
else:
397397
expected[i] = parse(val)
398398

399-
result = lib.string_to_datetime(strings)
399+
result = lib.array_to_datetime(strings)
400400
assert_almost_equal(result, expected)
401401

402402
result2 = to_datetime(strings)
@@ -452,6 +452,16 @@ def test_to_datetime_other_datetime64_units(self):
452452
value = Timestamp(scalar)
453453
self.assertEquals(value, as_obj)
454454

455+
def test_to_datetime_list_of_integers(self):
456+
rng = date_range('1/1/2000', periods=20)
457+
rng = DatetimeIndex(rng.values)
458+
459+
ints = list(rng.asi8)
460+
461+
result = DatetimeIndex(ints)
462+
463+
self.assert_(rng.equals(result))
464+
455465
def test_index_to_datetime(self):
456466
idx = Index(['1/1/2000', '1/2/2000', '1/3/2000'])
457467

pandas/tseries/tools.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,16 @@ def to_datetime(arg, errors='ignore', dayfirst=False):
8484
elif isinstance(arg, datetime):
8585
return arg
8686
elif isinstance(arg, Series):
87-
values = lib.string_to_datetime(com._ensure_object(arg.values),
88-
raise_=errors == 'raise',
89-
dayfirst=dayfirst)
87+
values = lib.array_to_datetime(com._ensure_object(arg.values),
88+
raise_=errors == 'raise',
89+
dayfirst=dayfirst)
9090
return Series(values, index=arg.index, name=arg.name)
9191
elif isinstance(arg, (np.ndarray, list)):
9292
if isinstance(arg, list):
9393
arg = np.array(arg, dtype='O')
94-
result = lib.string_to_datetime(com._ensure_object(arg),
95-
raise_=errors == 'raise',
96-
dayfirst=dayfirst)
94+
result = lib.array_to_datetime(com._ensure_object(arg),
95+
raise_=errors == 'raise',
96+
dayfirst=dayfirst)
9797
if com.is_datetime64_dtype(result):
9898
result = DatetimeIndex(result)
9999
return result

0 commit comments

Comments
 (0)