diff --git a/docs/source/python/dataset.rst b/docs/source/python/dataset.rst index 0c51458c8e6..0c4d53bbb91 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 607085f92b6..3c48b1576c6 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.