尝试将 DataFrame 写入羽毛时出错。 feather 是否支持列表列?
Error when trying to write DataFrame to feather. Does feather support list columns?
我同时使用 R 和 Python,我想写一个我的 pandas DataFrame 作为羽毛,这样我就可以在 R 中更轻松地使用它。但是,当我试着把它写成羽毛,我得到以下错误:
ArrowInvalid: trying to convert NumPy type float64 but got float32
我仔细检查了我的列类型,它们已经是 float 64:
In[1]
df.dtypes
Out[1]
id Object
cluster int64
vector_x float64
vector_y float64
无论使用 feather.write_dataframe(df, "path/df.feather")
还是 df.to_feather("path/df.feather")
我都会得到同样的错误。
我在 GitHub 上看到了这个,但不明白它是否相关:https://issues.apache.org/jira/browse/ARROW-1345 and https://github.com/apache/arrow/issues/1430
最后,我可以将它保存为 csv 并更改 R 中的列(或者只是在 Python 中进行整个分析),但我希望使用它。
编辑 1:
尽管下面有很好的建议,但仍然遇到同样的问题,所以更新我尝试过的内容。
df[['vector_x', 'vector_y', 'cluster']] = df[['vector_x', 'vector_y', 'cluster']].astype(float)
df[['doc_id', 'text']] = df[['doc_id', 'text']].astype(str)
df[['doc_vector', 'doc_vectors_2d']] = df[['doc_vector', 'doc_vectors_2d']].astype(list)
df.dtypes
Out[1]:
doc_id object
text object
doc_vector object
cluster float64
doc_vectors_2d object
vector_x float64
vector_y float64
dtype: object
编辑 2:
经过多次搜索,问题似乎出在我的簇列是由 int64 整数组成的列表类型。所以我想真正的问题是,羽化格式是否支持列表?
编辑 3:
顺便提一下,Feather 不支持像列表这样的嵌套数据类型,至少现在不支持。
您遇到的问题是 id Object
列。这些是 Python 个对象,它们不能以语言中性格式表示。这个羽毛(实际上是底层的 Apache Arrow / pyarrow
)试图猜测 id
列的数据类型。猜测是在它在列中看到的第一个对象上完成的。这些是 float64
numpy 标量。后来,你有 float32
个标量。 Arrow 没有将它们强制转换为某种类型,而是对类型更加严格并且失败了。
您应该能够通过确保所有列都具有 df['id'] = df['id'].astype(float)
.
的非对象数据类型来解决此问题
经过大量研究,简单的答案是 feather 不支持列表(或其他嵌套数据类型)列。
- Luckly, I got the reason of my feather IO error here.
- And I also got the solution for this problem, pandas.to_feather and read_feather are both based on pyarrow, and a column that contains lists as values is already support by pyarrow from 2019.
解法:
pip install pyarrow==latest # my version is 1.0.0 and it work
那么,还是用pd.to_feather("Filename") 和read_feather.
我同时使用 R 和 Python,我想写一个我的 pandas DataFrame 作为羽毛,这样我就可以在 R 中更轻松地使用它。但是,当我试着把它写成羽毛,我得到以下错误:
ArrowInvalid: trying to convert NumPy type float64 but got float32
我仔细检查了我的列类型,它们已经是 float 64:
In[1]
df.dtypes
Out[1]
id Object
cluster int64
vector_x float64
vector_y float64
无论使用 feather.write_dataframe(df, "path/df.feather")
还是 df.to_feather("path/df.feather")
我都会得到同样的错误。
我在 GitHub 上看到了这个,但不明白它是否相关:https://issues.apache.org/jira/browse/ARROW-1345 and https://github.com/apache/arrow/issues/1430
最后,我可以将它保存为 csv 并更改 R 中的列(或者只是在 Python 中进行整个分析),但我希望使用它。
编辑 1:
尽管下面有很好的建议,但仍然遇到同样的问题,所以更新我尝试过的内容。
df[['vector_x', 'vector_y', 'cluster']] = df[['vector_x', 'vector_y', 'cluster']].astype(float)
df[['doc_id', 'text']] = df[['doc_id', 'text']].astype(str)
df[['doc_vector', 'doc_vectors_2d']] = df[['doc_vector', 'doc_vectors_2d']].astype(list)
df.dtypes
Out[1]:
doc_id object
text object
doc_vector object
cluster float64
doc_vectors_2d object
vector_x float64
vector_y float64
dtype: object
编辑 2:
经过多次搜索,问题似乎出在我的簇列是由 int64 整数组成的列表类型。所以我想真正的问题是,羽化格式是否支持列表?
编辑 3:
顺便提一下,Feather 不支持像列表这样的嵌套数据类型,至少现在不支持。
您遇到的问题是 id Object
列。这些是 Python 个对象,它们不能以语言中性格式表示。这个羽毛(实际上是底层的 Apache Arrow / pyarrow
)试图猜测 id
列的数据类型。猜测是在它在列中看到的第一个对象上完成的。这些是 float64
numpy 标量。后来,你有 float32
个标量。 Arrow 没有将它们强制转换为某种类型,而是对类型更加严格并且失败了。
您应该能够通过确保所有列都具有 df['id'] = df['id'].astype(float)
.
经过大量研究,简单的答案是 feather 不支持列表(或其他嵌套数据类型)列。
- Luckly, I got the reason of my feather IO error here.
- And I also got the solution for this problem, pandas.to_feather and read_feather are both based on pyarrow, and a column that contains lists as values is already support by pyarrow from 2019.
解法:
pip install pyarrow==latest # my version is 1.0.0 and it work
那么,还是用pd.to_feather("Filename") 和read_feather.