Numpy Pytables表数据类型对齐

Numpy Pytables表数据类型对齐,numpy,memory-alignment,pytables,Numpy,Memory Alignment,Pytables,如果我创建以下对齐的Numpy数组 import numpy as np import tables as pt numrows = 10 dt = np.dtype([('date', [('year', '<i4'), ('month', '<i4'), ('day', '<i4')]), ('apples', '<f8'), ('oranges', '|S7'), ('pe

如果我创建以下对齐的Numpy数组

import numpy as np
import tables as pt
numrows = 10
dt = np.dtype([('date', [('year', '<i4'), ('month', '<i4'), ('day', '<i4')]), 
               ('apples', '<f8'), 
               ('oranges', '|S7'),
               ('pears', '<i4')], align=True) 
x = np.zeros(numrows, dtype=dt)
for d in x.dtype.descr:
    print d
数据类型包括这些额外的空格“|V4”和“|V1”

现在,当我使用相同的数据类型(Numpy风格)创建Pytable->table时,似乎失去了对齐

h5file = pt.open_file('mytable.h5', mode='w')
table = h5file.create_table('/', 'mytable', dt, filters=None, expectedrows=numrows, byteorder='little')
policy = table.row

for j in xrange(numrows):
    for field in table.colnames:
        if (field == 'date'):
            policy[field] = (2014, 1, 8)
        else:
            policy[field] = 0
    policy.append() 

table.flush()
mytable = h5file.root.mytable[:]
h5file.close()
for d in mytable.dtype.descr:
    print d
其输出为:

('date', [('year', '<i4'), ('month', '<i4'), ('day', '<i4')])
('apples', '<f8')
('oranges', '|S7')
('pears', '<i4')

('date',[('year',”默认情况下,PyTables不支持列的numpy void数据类型-请参阅
表的源代码。descr\u from\u dtype()
。但是,您可能可以通过将void替换为uint8s来诱使PyTables工作。这类似于:

dt = ...
expanded_dt = np.dtype(dt.descr)
newdt = []
for name, col in zip(expanded_dt.names, expanded_dt):
    if np.issubdtype(col, np.void):
        newdt.append([name, np.uint8(col.itemsize)])
    else:
        newdt.append([name, col])


newdt = np.dtype(newdt)
这将放入具有正确宽度的假列

dt = ...
expanded_dt = np.dtype(dt.descr)
newdt = []
for name, col in zip(expanded_dt.names, expanded_dt):
    if np.issubdtype(col, np.void):
        newdt.append([name, np.uint8(col.itemsize)])
    else:
        newdt.append([name, col])


newdt = np.dtype(newdt)