polars.from_dataframe#
- polars.from_dataframe(
- df: ArrowArrayExportable | ArrowStreamExportable,
- *,
- rechunk: bool = True,
Build a Polars DataFrame from any dataframe supporting the PyCapsule Interface.
Changed in version 2.0:
from_dataframeused to fall back to the Interchange Protocol, but this functionality has been removed. Theallow_copyparameter 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
Details on the PyCapsule Interface: https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html. Using a dedicated function like
from_pandas()orfrom_arrow()is a more efficient method of conversion and is recommended when possible.
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 │ └─────┴─────┴─────┘