Convert existing pandas tables
Migrating a pandas pipeline to Polars
Polars · Function
Converts a pandas DataFrame, Series, or Index into a Polars DataFrame or Series. Optional controls handle schema overrides, rechunking, NaN-to-null conversion, and index inclusion.
The function eagerly converts a pandas DataFrame, Series, or Index into a Polars DataFrame or Series. It can override inferred schemas, normalize chunks, turn floating-point NaN values into nulls, and optionally include a meaningful pandas index as a column.
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| Name | Type | Description |
|---|---|---|
data | pd.DataFrame | pd.Series[Any] | pd.Index[Any] | pd.DatetimeIndex | Pandas DataFrame, Series, Index, or DatetimeIndex to convert. |
schema_overrides | SchemaDict | None | Optional mapping of column or field names to Polars data types. |
rechunk | bool | Whether to rechunk the resulting data; defaults to true. |
nan_to_null | bool | Whether to convert NaN values to null; defaults to true. |
include_index | bool | Whether to include an eligible pandas index as a column; defaults to false. |
A Polars DataFrame for pandas DataFrame input, or a Polars Series for pandas Series or Index input.
schema_overrides to specify desired Polars types instead of relying entirely on inference.include_index=True; reset the index first if it must be preserved.import pandas as pd
import polars as pl
pdf = pd.DataFrame({"a": [1, 2], "b": ["x", "y"]})
df = pl.from_pandas(pdf, schema_overrides={"a": pl.Int32})
print(df)Converts a pandas DataFrame to Polars while overriding the inferred type of column a.
Migrating a pandas pipeline to Polars
Enforcing a stable numeric or temporal dtype during ingestion
Retaining a meaningful named pandas index during conversion
polars.from_pandas support?It accepts a pandas DataFrame, Series, Index, or DatetimeIndex and returns the corresponding Polars DataFrame or Series. The conversion is eager and may clone data.
Follow-up: How would you preserve a meaningful pandas index? Set include_index=True; for a default nameless index, call pandas reset_index() before conversion.
schema_overrides?schema_overrides supplies explicit Polars dtypes for selected columns or fields, replacing inferred types for those entries. The other columns retain inferred schemas.
Follow-up: Why might explicit overrides matter? They make downstream typing predictable when pandas or Arrow inference produces an undesired dtype.
rechunk=True requests contiguous memory for the resulting columns, while nan_to_null=True converts floating-point NaN values to Polars null values. include_index=True requests inclusion of a pandas index when it is eligible.
Follow-up: What is the limitation of include_index=True? It does not include a nameless default index; reset that index first.
polars.from_arrowpolars.from_dataframepolars.DataFrame.clonepolars.DataFrame.reset_index