我希望能够将numpy的使用
将NumPy数据追加到PyTables数据集
我的日期时间发生了什么?
我了解HDF5不提供对日期时间的本地支持。但我期望PyTables覆盖的额外元数据可以处理这个问题。
我的问题是如何在PyTables中存储包含日期时间的numpy记录数组?如何高效地从PyTables表中提取数据并保留日期时间到NumPy数组中?
通常的答案是使用Pandas,但我不想使用Pandas因为我没有索引,也不希望在数据集中存储索引。而且Pandas不允许您没有/存储索引。 (参见this question)
datetime64
数据存储在PyTables的Table
中,而且不使用Pandas。
目前为止我尝试过的
设置
In [1]: import tables as tb
In [2]: import numpy as np
In [3]: from datetime import datetime
创建数据
In [4]: data = [(1, datetime(2000, 1, 1, 1, 1, 1)), (2, datetime(2001, 2, 2, 2, 2, 2))]
In [5]: rec = np.array(data, dtype=[('a', 'i4'), ('b', 'M8[us]')])
In [6]: rec # a numpy array with my data
Out[6]:
array([(1, datetime.datetime(2000, 1, 1, 1, 1, 1)),
(2, datetime.datetime(2001, 2, 2, 2, 2, 2))],
dtype=[('a', '<i4'), ('b', '<M8[us]')])
使用Time64Col
描述符打开PyTables数据集
In [7]: f = tb.open_file('foo.h5', 'w') # New PyTables file
In [8]: d = f.create_table('/', 'bar', description={'a': tb.Int32Col(pos=0),
'b': tb.Time64Col(pos=1)})
In [9]: d
Out[9]:
/bar (Table(0,)) ''
description := {
"a": Int32Col(shape=(), dflt=0, pos=0),
"b": Time64Col(shape=(), dflt=0.0, pos=1)}
byteorder := 'little'
chunkshape := (5461,)
将NumPy数据追加到PyTables数据集
In [10]: d.append(rec)
In [11]: d
Out[11]:
/bar (Table(2,)) ''
description := {
"a": Int32Col(shape=(), dflt=0, pos=0),
"b": Time64Col(shape=(), dflt=0.0, pos=1)}
byteorder := 'little'
chunkshape := (5461,)
我的日期时间发生了什么?
In [12]: d[:]
Out[12]:
array([(1, 0.0), (2, 0.0)],
dtype=[('a', '<i4'), ('b', '<f8')])
我了解HDF5不提供对日期时间的本地支持。但我期望PyTables覆盖的额外元数据可以处理这个问题。
我的问题是如何在PyTables中存储包含日期时间的numpy记录数组?如何高效地从PyTables表中提取数据并保留日期时间到NumPy数组中?
通常的答案是使用Pandas,但我不想使用Pandas因为我没有索引,也不希望在数据集中存储索引。而且Pandas不允许您没有/存储索引。 (参见this question)
datetime64
所需的工作量要大得多,这相比于为PyTables提供Time64Col
元数据所提供的任何好处都微不足道。 - Phillip Cloud