gpt4 book ai didi

python - 将 pandas DataFrame 保存到 feather 时是否可以指定列类型?

转载 作者:太空宇宙 更新时间:2023-11-03 10:55:38 36 4
gpt4 key购买 nike

目前,如果一列碰巧只有空值,则会抛出异常并显示错误:

Invalid: Unable to infer type of object array, were all null

可以指定将使用的列的类型而不是推断类型吗?

版本:

feather-format==0.3.1
pandas==0.19.1

示例代码:

feather.write_dataframe(pandas.DataFrame([None]*5), '/tmp/test.feather')

最佳答案

None 更改(或替换)为 numpy.nan 它将起作用:

In [22]: feather.write_dataframe(pd.DataFrame([np.nan]*5), 'd:/temp/test.feather')

In [23]: feather.read_dataframe('d:/temp/test.feather')
Out[23]:
0
0 NaN
1 NaN
2 NaN
3 NaN
4 NaN

PS NumPy/Pandas/SciPy 等有自己的 Vanilla Python 的 None 表示 - NaN(不是数字)或 NaT (不是类似 DateTime 的数据类型的时间)

关于python - 将 pandas DataFrame 保存到 feather 时是否可以指定列类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41439564/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com