How to use polars dataframes with scikit-learn?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Scikit-learn expects NumPy arrays or pandas DataFrames as input. Polars DataFrames are not directly compatible, but converting between them is straightforward. You can call .to_numpy() on a Polars DataFrame to get a NumPy array, .to_pandas() to get a pandas DataFrame, or (since scikit-learn 1.4+) use Polars directly with the set_output API. This article covers each approach with practical machine learning examples.
Setup
Method 1: Convert to NumPy (.to_numpy())
The most common and reliable approach:
.to_numpy() creates a contiguous NumPy array, which is what scikit-learn uses internally. This is zero-overhead for numeric data with no null values.
Method 2: Convert to Pandas (.to_pandas())
Useful when scikit-learn utilities expect DataFrame features (column names, dtypes):
The conversion has overhead — Polars creates a new pandas DataFrame in memory. For large datasets, prefer .to_numpy().
Method 3: set_output API (scikit-learn 1.4+)
Scikit-learn 1.4+ supports Polars DataFrames directly through the set_output API:
This avoids manual conversion in preprocessing pipelines. Note that .fit() and .predict() still work with Polars since scikit-learn calls NumPy internally.
Full Pipeline Example
Handling Null Values
Polars uses null for missing values. NumPy converts these to NaN, which scikit-learn estimators do not accept by default:
Performance: Polars vs Pandas for ML Prep
For numeric-only DataFrames, .to_numpy() is significantly faster than .to_pandas() because it avoids constructing a pandas DataFrame.
Common Pitfalls
- Passing Polars DataFrames directly to
.fit(): Most scikit-learn estimators do not accept Polars DataFrames before version 1.4. You getTypeError: no valid checking.... Convert with.to_numpy()or.to_pandas()first. - Null values becoming NaN: Polars
nullbecomes NumPyNaN, which causesValueErrorin most scikit-learn estimators. Impute or drop nulls before conversion. - Losing column names with
.to_numpy(): NumPy arrays have no column names. If you need feature names (forColumnTransformeror feature importance), use.to_pandas()or pass column names manually. - Categorical columns in
.to_numpy(): String and categorical columns cannot be converted to a numeric NumPy array. Encode them first with Polars (df.with_columns(pl.col("city").cast(pl.Categorical))) or useOneHotEncoderin a pipeline. - Memory duplication: Both
.to_numpy()and.to_pandas()copy data. For very large datasets, this doubles memory usage. Consider processing in chunks or using Polars' lazy API to reduce the DataFrame before conversion.
Summary
- Use
.to_numpy()for the fastest conversion when column names are not needed - Use
.to_pandas()when scikit-learn utilities need column names (ColumnTransformer, feature importance) - Use
set_output(transform="polars")(scikit-learn 1.4+) to keep Polars DataFrames through pipelines - Handle null values before conversion — impute with
SimpleImputeror drop with.drop_nulls() - Encode categorical columns before calling
.to_numpy()— NumPy arrays are numeric only - For large datasets,
.to_numpy()is faster and uses less memory than.to_pandas()
Related reading
- How to use predict_generator with ImageDataGenerator?
- How to use py_func with a function that returns dict
- How to use repeat function when building data in Keras?
- How to use SageMaker Estimator for model training and saving
- How to use priority in celery task.apply_async
- how to use pymysql executemany insert many rows and get the ids of each rows
- How to use sample weights with tensorflow datasets?
- How to use sample weights with tensorflow datasets?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.