最初にcsvファイルをpandasデータフレームに読み込み、次にフィールドでいくつかの変換を行い、最後にto_recordsを使用してエクスポートし、必要なdtypeを指定して、csvファイルをpytablesにインポートしようとしています(データをpandas HDFStoreに保存しようとしましたが、あきらめました同様のエラーに直面したため)。pandas dataframe dtype を使用して dtypes を決定し、オブジェクト dtypes (文字列) について、最大文字列長を決定し、rec 配列を HDF に書き込む前に変換するときに 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 に設定すると、以下のエラーが発生します。チャンクサイズ 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
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=