polars.from_pandas

polars.from_pandas(df: pd.DataFrame, rechunk: bool = True, nan_to_none: bool = True) polars.internals.frame.DataFrame
polars.from_pandas(df: Union['pd.Series', 'pd.DatetimeIndex'], rechunk: bool = True, nan_to_none: bool = True) polars.internals.series.Series

Construct a Polars DataFrame or Series from a pandas DataFrame or Series.

This requires that pandas and pyarrow are installed.

Parameters
dfpandas DataFrame, Series, or DatetimeIndex

Data represented as a pandas DataFrame, Series, or DatetimeIndex.

rechunkbool, default True

Make sure that all data is contiguous.

nan_to_nonebool, default True

If data contains NaN values PyArrow will convert the NaN to None

Returns
DataFrame

Examples

Constructing a DataFrame from a pandas 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 pandas 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
]