polars.from_dataframe#

polars.from_dataframe(
df: SupportsInterchange | ArrowArrayExportable | ArrowStreamExportable,
*,
allow_copy: bool | None = None,
rechunk: bool = True,
) DataFrame[source]#

Build a Polars DataFrame from any dataframe supporting the PyCapsule Interface.

Changed in version 1.23.0: from_dataframe uses the PyCapsule Interface instead of the Dataframe Interchange Protocol for conversion, only using the latter as a fallback.

Parameters:
df

Object supporting the dataframe PyCapsule Interface.

allow_copy

Allow memory to be copied to perform the conversion. If set to False, may cause conversions that are not zero-copy to fail.

rechunkbool, default True

Make sure that all data is in contiguous memory.

Notes

Examples

Convert a pandas dataframe to Polars.

>>> import pandas as pd
>>> df_pd = pd.DataFrame({"a": [1, 2], "b": [3.0, 4.0], "c": ["x", "y"]})
>>> pl.from_dataframe(df_pd)
shape: (2, 3)
┌─────┬─────┬─────┐
│ a   ┆ b   ┆ c   │
│ --- ┆ --- ┆ --- │
│ i64 ┆ f64 ┆ str │
╞═════╪═════╪═════╡
│ 1   ┆ 3.0 ┆ x   │
│ 2   ┆ 4.0 ┆ y   │
└─────┴─────┴─────┘