Skip to content

Commit 07ae6e8

Browse files
committed
TST: eliminate copies in datetime64 serialization; don't copy data in DatetimeIndex, close #1320
1 parent 92cddfb commit 07ae6e8

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

pandas/io/pytables.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -910,11 +910,11 @@ def _delete_from_table(self, group, where = None):
910910

911911
def _convert_index(index):
912912
if isinstance(index, DatetimeIndex):
913-
converted = np.asarray(index, dtype='i8')
913+
converted = index.asi8
914914
return converted, 'datetime64', _tables().Int64Col()
915915
elif isinstance(index, (Int64Index, PeriodIndex)):
916916
atom = _tables().Int64Col()
917-
return np.asarray(index, dtype=np.int64), 'integer', atom
917+
return index.values, 'integer', atom
918918

919919
inferred_type = lib.infer_dtype(index)
920920

@@ -965,7 +965,7 @@ def _read_array(group, key):
965965

966966
def _unconvert_index(data, kind):
967967
if kind == 'datetime64':
968-
index = np.asarray(data, dtype='M8[ns]')
968+
index = DatetimeIndex(data)
969969
elif kind == 'datetime':
970970
index = np.array([datetime.fromtimestamp(v) for v in data],
971971
dtype=object)

pandas/tseries/index.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class TimeSeriesError(Exception):
121121

122122
_midnight = time(0, 0)
123123
_NS_DTYPE = np.dtype('M8[ns]')
124+
_INT64_DTYPE = np.dtype(np.int64)
124125

125126
class DatetimeIndex(Int64Index):
126127
"""
@@ -250,6 +251,8 @@ def __new__(cls, data=None,
250251
subarr = lib.cast_to_nanoseconds(data)
251252
else:
252253
subarr = data
254+
elif data.dtype == _INT64_DTYPE:
255+
subarr = data.view(_NS_DTYPE)
253256
elif issubclass(data.dtype.type, np.integer):
254257
subarr = np.array(data, dtype=_NS_DTYPE, copy=copy)
255258
else:

0 commit comments

Comments
 (0)