From 079bdb2676617c07dfb24f138bec2c64fcee65b4 Mon Sep 17 00:00:00 2001 From: parel Date: Thu, 3 Dec 2026 10:37:15 +0700 Subject: [PATCH] GH-27905: [Python][Dataset] Document dataset schema inference Document that ds.dataset() infers the dataset schema from a single fragment only, so an all-null column in that fragment becomes null-typed for the whole dataset and scanning other files can fail with an ArrowTypeError. Adds a note to the schema parameter and a short example to the Dataset guide. Assisted-by: Muse --- docs/source/python/dataset.rst | 22 ++++++++++++++++++++++ python/pyarrow/dataset.py | 6 +++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/docs/source/python/dataset.rst b/docs/source/python/dataset.rst index 0c51458c8e64..0c4d53bbb913 100644 --- a/docs/source/python/dataset.rst +++ b/docs/source/python/dataset.rst @@ -105,6 +105,28 @@ needed, it only crawls the directory to find all the files: b: double c: int64 +Note that the schema is inferred from a single file only, it is not unified +across all files. A column that holds only nulls in that file becomes +null-typed for the whole dataset, and reading the other files can then fail +with an ``ArrowTypeError`` if the column has values there. Passing an +explicit ``schema`` avoids this: + +.. code-block:: python + + >>> pq.write_table(pa.table({"a": [None, None]}), + ... base / "null.parquet") + >>> pq.write_table(pa.table({"a": ["x", "y"]}), + ... base / "str.parquet") + >>> dataset = ds.dataset([base / "null.parquet", base / "str.parquet"], + ... format="parquet") + >>> dataset.schema + a: null + >>> dataset = ds.dataset([base / "null.parquet", base / "str.parquet"], + ... format="parquet", + ... schema=pa.schema([("a", pa.string())])) + >>> dataset.schema + a: string + Using the :meth:`Dataset.to_table` method we can read the dataset (or a portion of it) into a pyarrow Table (note that depending on the size of your dataset this can require a lot of memory, see below on filtering / iterative loading): diff --git a/python/pyarrow/dataset.py b/python/pyarrow/dataset.py index 607085f92b62..3c48b1576c6d 100644 --- a/python/pyarrow/dataset.py +++ b/python/pyarrow/dataset.py @@ -618,7 +618,11 @@ def dataset(source, schema=None, format=None, filesystem=None, attempts will raise an error. schema : Schema, optional Optionally provide the Schema for the Dataset, in which case it will - not be inferred from the source. + not be inferred from the source. Otherwise the schema is inferred + from a single fragment only: a column that is all null in that + fragment becomes null-typed for the whole dataset, and scanning + files where the column has values can fail with an ArrowTypeError. + Pass ``schema`` explicitly to avoid this. format : FileFormat or str Currently "parquet", "ipc"/"arrow"/"feather", "csv", "json", and "orc" are supported. For Feather, only version 2 files are supported.