ray.data.读取_csv#

ray.data.read_csv(paths: str | List[str], *, filesystem: pyarrow.fs.FileSystem | None = None, parallelism: int = -1, ray_remote_args: Dict[str, Any] = None, arrow_open_stream_args: Dict[str, Any] | None = None, meta_provider: BaseFileMetadataProvider | None = None, partition_filter: PathPartitionFilter | None = None, partitioning: Partitioning = Partitioning(style='hive', base_dir='', field_names=None, filesystem=None), include_paths: bool = False, ignore_missing_paths: bool = False, shuffle: Literal['files'] | None = None, file_extensions: List[str] | None = None, concurrency: int | None = None, override_num_blocks: int | None = None, **arrow_csv_args) Dataset[源代码]#

从 CSV 文件创建一个 Dataset

示例

读取远程存储中的文件。

>>> import ray
>>> ds = ray.data.read_csv("s3://anonymous@ray-example-data/iris.csv")
>>> ds.schema()
Column             Type
------             ----
sepal length (cm)  double
sepal width (cm)   double
petal length (cm)  double
petal width (cm)   double
target             int64

读取多个本地文件。

>>> ray.data.read_csv( 
...    ["local:///path/to/file1", "local:///path/to/file2"])

从远程存储读取目录。

>>> ds = ray.data.read_csv("s3://anonymous@ray-example-data/iris-csv/")

读取使用不同分隔符的文件。有关 ParseOptions 的更多用途,请参见 https://arrow.apache.org/docs/python/generated/pyarrow.csv.ParseOptions.html # noqa: #501

>>> from pyarrow import csv
>>> parse_options = csv.ParseOptions(delimiter="\t")
>>> ds = ray.data.read_csv(
...     "s3://anonymous@ray-example-data/iris.tsv",
...     parse_options=parse_options)
>>> ds.schema()
Column        Type
------        ----
sepal.length  double
sepal.width   double
petal.length  double
petal.width   double
variety       string

将CSV文件中具有自定义格式的日期列进行转换。有关ConvertOptions的更多用法,请参见https://arrow.apache.org/docs/python/generated/pyarrow.csv.ConvertOptions.html # noqa: #501

>>> from pyarrow import csv
>>> convert_options = csv.ConvertOptions(
...     timestamp_parsers=["%m/%d/%Y"])
>>> ds = ray.data.read_csv(
...     "s3://anonymous@ray-example-data/dow_jones.csv",
...     convert_options=convert_options)

默认情况下,read_csv() 从文件路径解析 Hive 风格的分区。如果你的数据遵循不同的分区方案,请设置 partitioning 参数。

>>> ds = ray.data.read_csv("s3://anonymous@ray-example-data/year=2022/month=09/sales.csv")
>>> ds.take(1)
[{'order_number': 10107, 'quantity': 30, 'year': '2022', 'month': '09'}]

默认情况下,read_csv() 会读取文件路径中的所有文件。如果你想按文件扩展名过滤文件,请设置 file_extensions 参数。

从目录中只读取 *.csv 文件。

>>> ray.data.read_csv("s3://anonymous@ray-example-data/different-extensions/",
...     file_extensions=["csv"])
Dataset(num_rows=?, schema={a: int64, b: int64})
参数:
  • paths – 单个文件或目录,或文件或目录路径的列表。路径列表可以同时包含文件和目录。

  • filesystem – 用于读取的 PyArrow 文件系统实现。这些文件系统在 pyarrow 文档 中指定。如果你需要为文件系统提供特定的配置,请指定此参数。默认情况下,文件系统会根据路径的方案自动选择。例如,如果路径以 s3:// 开头,则使用 S3FileSystem

  • parallelism – 此参数已弃用。请使用 override_num_blocks 参数。

  • ray_remote_args – 传递给读取任务中 remote() 的 kwargs。

  • arrow_open_stream_args – 传递给 pyarrow.fs.FileSystem.open_input_file 的 kwargs,用于打开输入文件进行读取。

  • meta_provider – 一个 文件元数据提供者 。自定义元数据提供者可能能够更快和/或更准确地解析文件元数据。在大多数情况下,您不需要设置此项。如果为 None ,此函数使用系统选择的实现。

  • partition_filter – 一个 PathPartitionFilter。与自定义回调一起使用,以仅读取数据集的选定分区。默认情况下,不筛选任何文件。

  • partitioning – 一个描述路径组织方式的 Partitioning 对象。默认情况下,此函数解析 Hive 风格的分区

  • include_paths – 如果 True,则包含每个文件的路径。文件路径存储在 'path' 列中。

  • ignore_missing_paths – 如果为 True,则忽略 paths 中未找到的任何文件路径。默认为 False。

  • shuffle – 如果设置为“files”,在读取前随机打乱输入文件的顺序。默认不进行打乱,使用 None

  • arrow_csv_args – CSV 读取选项,传递给 pyarrow.csv.open_csv 以打开 CSV 文件。

  • file_extensions – 用于筛选文件的文件扩展名列表。

  • concurrency – Ray 任务的最大并发运行数量。设置此项以控制并发运行的任务数量。这不会改变运行的总任务数或输出的总块数。默认情况下,并发性是根据可用资源动态决定的。

  • override_num_blocks – 覆盖所有读取任务的输出块数量。默认情况下,输出块的数量是根据输入数据大小和可用资源动态决定的。在大多数情况下,您不应手动设置此值。

返回:

Dataset 从指定路径读取记录并生成数据集。