polars.from_dataframe#

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

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

Changed in version 2.0: from_dataframe used to fall back to the Interchange Protocol, but this functionality has been removed. The allow_copy parameter was removed along with it.

Parameters:
df

Object supporting the dataframe PyCapsule Interface.

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   │
└─────┴─────┴─────┘