使用 pyarrow 读取 CSV 文件时,如何为所有列指定 dtype?
我想用 pyarrow 读取一个大的 CSV 文件。我所有的列都是 float64 的。但 pyarrow 似乎正在推断 int64。
如何为所有列指定数据类型?
import gcsfs
import pyarrow.dataset as ds
fs = gcsfs.GCSFileSystem(project='my-google-cloud-project')
my_dataset = ds.dataset("bucket/foo/bar.csv", format="csv", filesystem=fs)
my_dataset.to_table()
其产生:
ArrowInvalid Traceback (most recent call last)
........py in <module>
----> 65 my_dataset.to_table()
File /opt/conda/envs/py39/lib/python3.9/site-packages/pyarrow/_dataset.pyx:491, in pyarrow._dataset.Dataset.to_table()
File /opt/conda/envs/py39/lib/python3.9/site-packages/pyarrow/_dataset.pyx:3235, in pyarrow._dataset.Scanner.to_table()
File /opt/conda/envs/py39/lib/python3.9/site-packages/pyarrow/error.pxi:143, in pyarrow.lib.pyarrow_internal_check_status()
File /opt/conda/envs/py39/lib/python3.9/site-packages/pyarrow/error.pxi:99, in pyarrow.lib.check_status()
ArrowInvalid: In CSV column #172: Row #28: CSV conversion error to int64: invalid value '6.58841482364418'
I wanna read a big CSV file with pyarrow. All my columns are float64's. But pyarrow seems to be inferring int64.
How do I specify a dtype for all columns?
import gcsfs
import pyarrow.dataset as ds
fs = gcsfs.GCSFileSystem(project='my-google-cloud-project')
my_dataset = ds.dataset("bucket/foo/bar.csv", format="csv", filesystem=fs)
my_dataset.to_table()
which produces:
ArrowInvalid Traceback (most recent call last)
........py in <module>
----> 65 my_dataset.to_table()
File /opt/conda/envs/py39/lib/python3.9/site-packages/pyarrow/_dataset.pyx:491, in pyarrow._dataset.Dataset.to_table()
File /opt/conda/envs/py39/lib/python3.9/site-packages/pyarrow/_dataset.pyx:3235, in pyarrow._dataset.Scanner.to_table()
File /opt/conda/envs/py39/lib/python3.9/site-packages/pyarrow/error.pxi:143, in pyarrow.lib.pyarrow_internal_check_status()
File /opt/conda/envs/py39/lib/python3.9/site-packages/pyarrow/error.pxi:99, in pyarrow.lib.check_status()
ArrowInvalid: In CSV column #172: Row #28: CSV conversion error to int64: invalid value '6.58841482364418'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Pyarrow 的数据集模块以块的形式读取 CSV 文件(我认为默认值为 1MB),并并行处理这些块。这使得列推断有点棘手,它通过使用第一个块来推断数据类型来处理这个问题。因此,当文件的第一个块的列看起来是整数但在未来的块中该列具有十进制值时,您收到的错误非常常见。
如果您事先知道列名,那么您可以指定列的数据类型:
如果您不知道列名,那么事情会有点棘手。然而,听起来所有列都是
float64
。在这种情况下,由于您只有一个文件,您可能可以执行以下操作作为解决方法:这有效,因为我们调用
pa.dataset(...)
两次,并且它会有一小部分的开销。这是因为每次我们调用 pa.dataset(...) pyarrow 都会打开数据集中第一个文件的第一个块来确定模式(这就是为什么我们可以使用 dataset .schema)如果您有多个具有不同列的文件,那么此方法将不起作用。在这种情况下,我建议向 Arrow user@ 邮件列表发送邮件,我们可以就解决问题的不同方法进行更一般的讨论。
Pyarrow's dataset module reads CSV files in chunks (the default is 1MB I think) and it processes those chunks in parallel. This makes column inference a bit tricky and it handles this by using the first chunk to infer data types. So the error you are getting is very common when the first chunk of the file has a column that looks integral but in future chunks the column has decimal values.
If you know the column names in advance then you can specify the data types of the columns:
If you don't know the column names then things are a bit trickier. However, it sounds like ALL columns are
float64
. In that case, since you only have one file, you can probably do something like this as a workaround:This works because we call
pa.dataset(...)
twice and it will have a small bit of overhead. This is because each time we callpa.dataset(...)
pyarrow will open the first chunk of the first file in the dataset to determine the schema (this is why we can usedataset.schema
)If you have multiple files with different columns then this approach won't work. In that case I'd recommend mailing the Arrow user@ mailing list and we can have a more general discussion about different ways to solve the problem.