我试图将csv文件导入pytables,首先将其读入pandas数据框,然后在字段上进行一些转换,最后使用to_records导出它并指定我想要的dtypes(我已经尝试将数据保存到pandas HDFStore但是当我遇到类似的错误时放弃了)。我已经确定了dtypes,通过使用pandas dataframe dtype和对象dtypes(字符串),我确定了最大字符串长度,并在写入HDF之前转换rec数组时指定了我的dtype。
f = open_file('FLN3.h5',mode='w')
for num, chunk in enumerate(read_csv('DATA.csv',dtype=dt1, iterator=True, chunksize=250000)):
#Some manipulation happenst to varioud columns
print str(now()),str(num), "Completed Date Parse"
df = chunk.to_records().astype(dt)
if num == 0:
f.create_table('/','df',description = df)
f.flush()
else:
f.get_node('/','df').append(df)
f.flush()
del df
chunk = 0
print str(now()),str(num), "Completed Write"
f.close()
当我使用read_csv chunksize = 100000运行上述代码时,操作将顺利进行。但是当我将chunksize设置为250000时,我会遇到下面的错误。请注意,当我使用chunksize 100000运行时,记录将写在远高于前500000条记录的位置,表明记录本身没有问题,numpy rec数组也不会抛出任何错误,因为它的dtype已经存在astype(dt)
中特别提供的。问题似乎在于pytables写操作本身。
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Thu Aug 22 15:50:53 2013 0 Read CSV
Thu Aug 22 15:51:09 2013 0 Completed Date Parse
Thu Aug 22 15:51:25 2013 0 Completed Write
Thu Aug 22 15:51:36 2013 1 Read CSV
Thu Aug 22 15:51:52 2013 1 Completed Date Parse
Traceback (most recent call last):
File "D:/AIMS/csvtohdf2.py", line 144, in <module>
f.get_node('/','df').append(df)
File "D:\Python27\lib\site-packages\tables\table.py", line 2229, in append
"The error was: <%s>" % (str(self), exc))
ValueError: rows parameter cannot be converted into a recarray object compliant with table '/df (Table(250000,)) '''. The error was: <>
>>>
这是tables.test()输出
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
PyTables version: 3.0.0
HDF5 version: 1.8.10-patch1
NumPy version: 1.7.1
Numexpr version: 2.1 (not using Intel's VML/MKL)
Zlib version: 1.2.3 (in Python interpreter)
LZO version: 2.06 (Aug 12 2011)
BZIP2 version: 1.0.6 (6-Sept-2010)
Blosc version: 1.2.3 (2013-05-17)
Cython version: 0.19.1
Python version: 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]
Byte-ordering: little
Detected cores: 4
Default encoding: ascii
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=