使用 pyarrow 读取 CSV 文件时,如何为所有列指定 dtype?

发布于 2025-01-15 18:30:42 字数 1079 浏览 2 评论 0原文

我想用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不如归去 2025-01-22 18:30:42

Pyarrow 的数据集模块以块的形式读取 CSV 文件(我认为默认值为 1MB),并并行处理这些块。这使得列推断有点棘手,它通过使用第一个块来推断数据类型来处理这个问题。因此,当文件的第一个块的列看起来是整数但在未来的块中该列具有十进制值时,您收到的错误非常常见。

如果您事先知道列名,那么您可以指定列的数据类型:

import pyarrow as pa
import pyarrow.csv as csv
import pyarrow.dataset as ds

column_types = {'a': pa.float64(), 'b': pa.float64(), 'c': pa.float64()}
convert_options = csv.ConvertOptions(column_types=column_types)
custom_csv_format = ds.CsvFileFormat(convert_options=convert_options)
dataset = ds.dataset('/tmp/foo.csv', format=custom_csv_format)

如果您不知道列名,那么事情会有点棘手。然而,听起来所有列都是float64。在这种情况下,由于您只有一个文件,您可能可以执行以下操作作为解决方法:

dataset = ds.dataset('/tmp/foo.csv', format='csv')
column_types = {}
for field in dataset.schema:
  column_types[field.name] = pa.float64()
# Now use column_types as above

这有效,因为我们调用 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:

import pyarrow as pa
import pyarrow.csv as csv
import pyarrow.dataset as ds

column_types = {'a': pa.float64(), 'b': pa.float64(), 'c': pa.float64()}
convert_options = csv.ConvertOptions(column_types=column_types)
custom_csv_format = ds.CsvFileFormat(convert_options=convert_options)
dataset = ds.dataset('/tmp/foo.csv', format=custom_csv_format)

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:

dataset = ds.dataset('/tmp/foo.csv', format='csv')
column_types = {}
for field in dataset.schema:
  column_types[field.name] = pa.float64()
# Now use column_types as above

This works because we call pa.dataset(...) twice and it will have a small bit of overhead. This is because each time we call pa.dataset(...) pyarrow will open the first chunk of the first file in the dataset to determine the schema (this is why we can use dataset.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文