Skip to content

Commit c63b785

Browse files
committed
ENH: cythonize timestamp conversion in HDFStore
1 parent 92fb29a commit c63b785

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

pandas/io/pytables.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -771,20 +771,13 @@ def _unconvert_index_legacy(data, kind, legacy=False):
771771
def _maybe_convert(values, val_kind):
772772
if _need_convert(val_kind):
773773
conv = _get_converter(val_kind)
774-
conv = np.frompyfunc(conv, 1, 1)
774+
# conv = np.frompyfunc(conv, 1, 1)
775775
values = conv(values)
776776
return values
777777

778778
def _get_converter(kind):
779779
if kind == 'datetime':
780-
cache = {}
781-
def convert_datetime(x):
782-
if x in cache:
783-
return cache[x]
784-
else:
785-
cache[x] = result = datetime.fromtimestamp(x)
786-
return result
787-
return convert_datetime
780+
return lib.convert_timestamps
788781
else: # pragma: no cover
789782
raise ValueError('invalid kind %s' % kind)
790783

pandas/src/tseries.pyx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,29 @@ def has_infs_f8(ndarray[float64_t] arr):
452452
return True
453453
return False
454454

455+
def convert_timestamps(ndarray values):
456+
cdef:
457+
object val, f, result
458+
dict cache = {}
459+
Py_ssize_t i, n = len(values)
460+
ndarray[object] out
461+
462+
# for HDFStore, a bit temporary but...
463+
464+
from datetime import datetime
465+
f = datetime.fromtimestamp
466+
467+
out = np.empty(n, dtype='O')
468+
469+
for i in range(n):
470+
val = util.get_value_1d(values, i)
471+
if val in cache:
472+
out[i] = cache[val]
473+
else:
474+
cache[val] = out[i] = f(val)
475+
476+
return out
477+
455478
# cdef class TypeConverter:
456479
# cdef:
457480
# cpython.PyTypeObject* klass_type

0 commit comments

Comments
 (0)