我有一个大的Pandas数据框(〜15GB,8300万行),我有兴趣另存为h5
(或feather
)文件。一列包含数字的长ID字符串,该字符串应具有字符串/对象类型。但是,即使我确保熊猫将所有列都解析为object
:
df = pd.read_csv('data.csv', dtype=object)
print(df.dtypes) # sanity check
df.to_hdf('df.h5', 'df')
> client_id object
event_id object
account_id object
session_id object
event_timestamp object
# etc...
我收到此错误:
File "foo.py", line 14, in <module>
df.to_hdf('df.h5', 'df')
File "/shared_directory/projects/env/lib/python3.6/site-packages/pandas/core/generic.py", line 1996, in to_hdf
return pytables.to_hdf(path_or_buf, key, self, **kwargs)
File "/shared_directory/projects/env/lib/python3.6/site-packages/pandas/io/pytables.py", line 279, in to_hdf
f(store)
File "/shared_directory/projects/env/lib/python3.6/site-packages/pandas/io/pytables.py", line 273, in <lambda>
f = lambda store: store.put(key, value, **kwargs)
File "/shared_directory/projects/env/lib/python3.6/site-packages/pandas/io/pytables.py", line 890, in put
self._write_to_group(key, value, append=append, **kwargs)
File "/shared_directory/projects/env/lib/python3.6/site-packages/pandas/io/pytables.py", line 1367, in _write_to_group
s.write(obj=value, append=append, complib=complib, **kwargs)
File "/shared_directory/projects/env/lib/python3.6/site-packages/pandas/io/pytables.py", line 2963, in write
self.write_array('block%d_values' % i, blk.values, items=blk_items)
File "/shared_directory/projects/env/lib/python3.6/site-packages/pandas/io/pytables.py", line 2730, in write_array
vlarr.append(value)
File "/shared_directory/projects/env/lib/python3.6/site-packages/tables/vlarray.py", line 547, in append
self._append(nparr, nobjects)
File "tables/hdf5extension.pyx", line 2032, in tables.hdf5extension.VLArray._append
OverflowError: value too large to convert to int
显然,它试图将其转换为int并失败。
运行df.to_feather()
时遇到类似的问题:
df.to_feather('df.feather')
File "/shared_directory/projects/env/lib/python3.6/site-packages/pandas/core/frame.py", line 1892, in to_feather
to_feather(self, fname)
File "/shared_directory/projects/env/lib/python3.6/site-packages/pandas/io/feather_format.py", line 83, in to_feather
feather.write_dataframe(df, path)
File "/shared_directory/projects/env/lib/python3.6/site-packages/pyarrow/feather.py", line 182, in write_feather
writer.write(df)
File "/shared_directory/projects/env/lib/python3.6/site-packages/pyarrow/feather.py", line 93, in write
table = Table.from_pandas(df, preserve_index=False)
File "pyarrow/table.pxi", line 1174, in pyarrow.lib.Table.from_pandas
File "/shared_directory/projects/env/lib/python3.6/site-packages/pyarrow/pandas_compat.py", line 501, in dataframe_to_arrays
convert_fields))
File "/usr/lib/python3.6/concurrent/futures/_base.py", line 586, in result_iterator
yield fs.pop().result()
File "/usr/lib/python3.6/concurrent/futures/_base.py", line 425, in result
return self.__get_result()
File "/usr/lib/python3.6/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/usr/lib/python3.6/concurrent/futures/thread.py", line 56, in run
result = self.fn(*self.args, **self.kwargs)
File "/shared_directory/projects/env/lib/python3.6/site-packages/pyarrow/pandas_compat.py", line 487, in convert_column
raise e
File "/shared_directory/projects/env/lib/python3.6/site-packages/pyarrow/pandas_compat.py", line 481, in convert_column
result = pa.array(col, type=type_, from_pandas=True, safe=safe)
File "pyarrow/array.pxi", line 191, in pyarrow.lib.array
File "pyarrow/array.pxi", line 78, in pyarrow.lib._ndarray_to_array
File "pyarrow/error.pxi", line 85, in pyarrow.lib.check_status
pyarrow.lib.ArrowInvalid: ('Could not convert 1542852887489 with type str: tried to convert to double', 'Conversion failed for column session_id with type object')
所以:
答案 0 :(得分:1)
HDF5不是此用例的合适解决方案。如果要在单个结构中存储许多数据帧,则hdf5是更好的解决方案。打开文件时,它会有更多开销,然后使您可以有效地加载每个数据帧,也可以轻松地加载它们的切片。应该将其视为存储数据帧的文件系统。
对于时间序列事件的单个数据帧,建议的格式将是Apache Arrow项目格式之一,即feather
或parquet
。人们应该将它们视为基于列的(压缩)csv文件。在What are the differences between feather and parquet?下很好地列出了这两者之间的特殊权衡。
要考虑的一个特殊问题是数据类型。由于feather
并非旨在通过压缩来优化磁盘空间,因此它可以为larger variety of data types提供支持。尽管parquet
试图提供非常有效的压缩,但它只能支持limited subset,这将使其能够更好地处理数据压缩。
答案 1 :(得分:0)
已经阅读了有关该主题的文章,看来问题是在处理string
类型的列。我的string
列包含全数字字符串和带字符的字符串的混合。 Pandas具有灵活的选项,可以将字符串保留为object
,而没有声明的类型,但是当序列化为hdf5
或feather
时,列的内容将转换为类型({{1 }}或str
,并且不能混用。当面对足够大的混合类型库时,这两个库都将失败。
对于文本数据,除了double
/ hdf5
以外,还有其他推荐的解决方案,包括:
feather
json
(请注意,熊猫0.25 msgpack
已弃用)read_msgpack
(已知security issues,所以要小心-但对于内部存储/传输数据帧应该没问题)pickle
,是Apache Arrow生态系统的一部分。 Here是Matthew Rocklin(parquet
开发人员之一)对dask
和msgpack
进行比较的答案。他在自己的blog上进行了比较广泛的比较。