polars.from_pandas
-
Construct a Polars DataFrame or Series from a pandas DataFrame, Series, or Index.
This operation may clone data. If you want to ensure that in-place modifications of the output don’t affect the input, you may want to consider one of the following:
- Enable Copy-On-Write in pandas.
- Call
DataFrame.clone()on the output offrom_pandas.
This requires that
pandasandpyarroware installed.- Parameters:
-
-
datapandas.DataFrame or pandas.Series or pandas.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
NaNvalues PyArrow will convert theNaNtoNone -
include_indexbool, default False -
Load any non-default pandas indexes as columns.
Note
If the input is a pandas
DataFrameand has a nameless index which just enumerates the rows, then it will not be included in the result, regardless of this parameter. If you want to be sure to include it, please call.reset_index()prior to calling this function.
-
- Returns:
-
- DataFrame
Examples
Constructing a
DataFramefrom 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
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 ]
polars.from_pandas(
data: pd.DataFrame | pd.Series[Any] | pd.Index[Any] | pd.DatetimeIndex,
*,
schema_overrides: SchemaDict | None = None,
rechunk: bool = True,
nan_to_null: bool = True,
include_index: bool = False,
) → DataFrame | Series
© 2020 Ritchie Vink
© 2022 Polars contributors
Licensed under the MIT License.
https://docs.pola.rs/api/python/stable/reference/api/polars.from_pandas.html