polars.from_arrow#

polars.from_arrow(
data: pa.Table | pa.Array | pa.ChunkedArray | pa.RecordBatch | Iterable[pa.RecordBatch | pa.Table] | ArrowArrayExportable | ArrowStreamExportable,
schema: SchemaDefinition | None = None,
*,
schema_overrides: SchemaDict | None = None,
rechunk: bool = True,
) DataFrame | Series[source]#

Create a DataFrame or Series from an Arrow Table or Array.

This operation will be zero copy for the most part. Types that are not supported by Polars may be cast to the closest supported type.

Hint: You can also directly pass arrow tables to pl.DataFrame() / arrow arrays to pl.Series() if the output type is known to avoid typing issues.

Parameters:
datapyarrow.Table, pyarrow.Array, one or more pyarrow.RecordBatch

Data representing an Arrow Table, Array, sequence of RecordBatches or Tables, or other object that supports the Arrow PyCapsule interface.

schemaSequence of str, (str,DataType) pairs, or a {str:DataType,} dict

The DataFrame schema may be declared in several ways:

  • As a dict of {name:type} pairs; if type is None, it will be auto-inferred.

  • As a list of column names; in this case types are automatically inferred.

  • As a list of (name,type) pairs; this is equivalent to the dictionary form.

Schema entries are applied positionally, following the order of the Python dictionary. The provided schema takes precedence over the schema of the underlying Arrow data. As such, if the provided schema names do not match the underlying Arrow data, the column names of the Arrow data will be discarded in favor of the names set in this argument. The number of names given in the schema must match the underlying data dimensions.

schema_overridesdict, default None

Support type specification or override of one or more columns; note that any dtypes inferred from the schema param will be overridden.

rechunkbool, default True

Make sure that all data is in contiguous memory.

Returns:
DataFrame or Series

Examples

Constructing a DataFrame from an Arrow Table:

>>> import pyarrow as pa
>>> data = pa.table({"a": [1, 2, 3], "b": [4, 5, 6]})
>>> pl.from_arrow(data)
shape: (3, 2)
┌─────┬─────┐
│ a   ┆ b   │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 1   ┆ 4   │
│ 2   ┆ 5   │
│ 3   ┆ 6   │
└─────┴─────┘

Constructing a Series from an Arrow Array:

>>> import pyarrow as pa
>>> data = pa.array([1, 2, 3])
>>> pl.from_arrow(data, schema={"s": pl.Int32})
shape: (3,)
Series: 's' [i32]
[
    1
    2
    3
]