polars.from_pandas#
- polars.from_pandas(
- data: DataFrame,
- *,
- schema_overrides: SchemaDict | None = None,
- rechunk: bool = True,
- nan_to_null: bool = True,
- include_index: bool = False,
- polars.from_pandas(
- data: pd.Series[Any] | pd.Index[Any],
- *,
- schema_overrides: SchemaDict | None = None,
- rechunk: bool = True,
- nan_to_null: bool = True,
- include_index: bool = False,
Construct a Polars DataFrame or Series from a pandas DataFrame or Series.
This operation clones data.
This requires that
pandas
andpyarrow
are installed.- Parameters:
- data
pandas.DataFrame
orpandas.Series
orpandas.Index
Data represented as a pandas DataFrame, Series, or Index.
- schema_overridesdict, default None
Support override of inferred types for one or more columns.
- rechunkbool, default True
Make sure that all data is in contiguous memory.
- nan_to_nullbool, default True
If data contains
NaN
values PyArrow will convert theNaN
toNone
- include_indexbool, default False
Load any non-default pandas indexes as columns.
- data
- Returns:
- DataFrame
Examples
Constructing a
DataFrame
from apandas.DataFrame
:>>> import pandas as pd >>> pd_df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=["a", "b", "c"]) >>> df = pl.from_pandas(pd_df) >>> df shape: (2, 3) ┌─────┬─────┬─────┐ │ a ┆ b ┆ c │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 │ ╞═════╪═════╪═════╡ │ 1 ┆ 2 ┆ 3 │ │ 4 ┆ 5 ┆ 6 │ └─────┴─────┴─────┘
Constructing a Series from a
pd.Series
:>>> import pandas as pd >>> pd_series = pd.Series([1, 2, 3], name="pd") >>> df = pl.from_pandas(pd_series) >>> df shape: (3,) Series: 'pd' [i64] [ 1 2 3 ]