polars.from_dataframe#
- polars.from_dataframe(df: SupportsInterchange, *, allow_copy: bool = True) DataFrame [source]#
Build a Polars DataFrame from any dataframe supporting the interchange protocol.
- Parameters:
- df
Object supporting the dataframe interchange protocol, i.e. must have implemented the
__dataframe__
method.- allow_copy
Allow memory to be copied to perform the conversion. If set to False, causes conversions that are not zero-copy to fail.
Notes
Details on the Python dataframe interchange protocol: https://data-apis.org/dataframe-protocol/latest/index.html
Using a dedicated function like
from_pandas()
orfrom_arrow()
is a more efficient method of conversion.Examples
Convert a pandas dataframe to Polars through the interchange protocol.
>>> import pandas as pd >>> df_pd = pd.DataFrame({"a": [1, 2], "b": [3.0, 4.0], "c": ["x", "y"]}) >>> dfi = df_pd.__dataframe__() >>> pl.from_dataframe(dfi) shape: (2, 3) ┌─────┬─────┬─────┐ │ a ┆ b ┆ c │ │ --- ┆ --- ┆ --- │ │ i64 ┆ f64 ┆ str │ ╞═════╪═════╪═════╡ │ 1 ┆ 3.0 ┆ x │ │ 2 ┆ 4.0 ┆ y │ └─────┴─────┴─────┘