pandas 附加 HDFStore 错误 - “无法序列化列”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18805426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:09:26  来源:igfitidea点击:

HDFStore error appending - "Cannot serialize the column"

pythonpandas

提问by zio

I have a dataframe, df:

我有一个数据框,df:

    datetime                      bid      ask     bidvolume  askvolume
0   2007-03-30 21:00:00.332000   1.9682   1.9678       4         0.8

Trying to append this to a new datastore. The datastore does not exist so I use the following to create and append the data;

尝试将此附加到新的数据存储。数据存储不存在,所以我使用以下内容来创建和附加数据;

store = pd.HDFStore(storePath,mode='w')
store.append('data',df)
store.close()

I get this error: on the store.appendline.

我收到这个错误:store.append在线。

TypeError: Cannot serialize the column [bid] because
its data contents are [floating] object dtype

How do I get the data to store properly?

如何让数据正确存储?

采纳答案by Phillip Cloud

Please note: the following method convert_objects()is now deprecated and may not workCall DataFrame.convert_objects():

请注意:以下方法convert_objects()现已弃用,可能无法正常工作Call DataFrame.convert_objects()

df = DataFrame(randn(10, 1), dtype=object).convert_objects()
df.to_hdf('/tmp/blah.h5', 'df', append=True)

It might be worth checking to see if you can get your data in the correct format before you start saving to HDF5. For example, wherever dfis created, convert the objects there, instead of converting them when you save. In general, operations in pandas will be very cumbersome with a Seriesof floats with a dtypeof object. Your life will be much easier if you convert your object arrays (where possible) as soon as you need to do anything with them.

在开始保存到 HDF5 之前,可能值得检查一下是否可以以正确的格式获取数据。例如,无论在哪里df创建,都在那里转换对象,而不是在保存时转换它们。一般来说,pandas 中的操作会非常繁琐,a Seriesof floats 和 a dtypeof object。如果您在需要对对象数组执行任何操作时立即转换对象数组(在可能的情况下),您的生活会容易得多。