Repository navigation
Open
Labels
Description
Describe the workflow you want to enable
using polars data (DataFrame, Series) is already supported in many places which is awesome, thank you!!
But in many places there are still
- errors / crashes -> required conversion to numpy/pandas
- warnings -> requires conversion to numpy/pandas
- linting/type problems -> requires updates to typing signalture?
Examples
Code
import pandas as pd
import polars as pl
from sklearn.datasets import make_classification
from sklearn.inspection import permutation_importance
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score, cross_validate
X, y = make_classification()
# X = pd.DataFrame(X)
# y = pd.Series(y)
# X = pl.DataFrame(X)
# y = pl.Series(y)
clf = LogisticRegression()
clf.fit(
X=X,
y=y,
)
clf.score(
X=X, # Lint/Type Problem
y=y,
)
cross_val_score(
estimator=clf,
X=X, # Lint/Type Problem
y=y, # ERROR with polars
)
cross_validate(
estimator=clf,
X=X, # Lint/Type Problem
y=y, # ERROR with polars
)
permutation_importance(
estimator=clf,
X=X, # WARNING with polars + Lint/Type Problem
y=y,
)
clf.predict(X)Errors / crashes using polars
Both cross_val_score and cross_validate crash using polars Series with message:
TypeError: cannot usegetitemon Series of dtype Int32 with argument (array([18, 21, 22, 23, 24, 25,...
Temporary solution
use
to_numpy()-> works with numpy arrayto_pandas()-> works with pandas Series
Warnings
permutation_importance creates "UserWarnings" using polars DataFrame with message:
UserWarning: X does not have valid feature names, but LogisticRegression was fitted with feature names
Temporary Solution
use
to_pandas()-> works with pandas DataFrame
Linting / Type problems
numpy arrays and pandas DataFrame have "full support" while polars looks a little sad 😆
Describe your proposed solution
would love to see further support for polars DataFrame / Series types.
Not sure why some things error, because I thought polars provides all the required apis.
Any way to assist / help?
Describe alternatives you've considered, if relevant
No response
Additional context
No response
Reacted by gona11 and Michael Mayer
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDiscussion

Activity
glemaitre commentedon Feb 20, 2024
Wild guess would be that
_safe_indexingdoes not support the polars maybe. I need to check deeper.lesteve commentedon Feb 21, 2024
About linting / typing issues you mentioned, the type annotations are not maintained by the scikit-learn project so they come from somewhere else, maybe https://github.com/microsoft/python-type-stubs/tree/main/stubs/sklearn?
Charlie-XIAO commentedon Feb 21, 2024
It seems that
_safe_indexinguses polars indexing only for polars dataframes but not for polars series, which causescross_validateandcross_val_score(that usescross_validate) to fail.The warning of
permutation_importanceis caused by converting polars DataFrame to numpy array (while we detect pandas DataFrame and avoid such a conversion).[-]Improve "polars" integration (error, warning & linting examples)[/-][+]permutation_importance with polars warns on invalid feature names[/+]jeremiedbb commentedon Apr 7, 2026
The only remaining bug here is the warning on invalid feature names raised by permuation_importance for polars dataframes. I updated the title of this issue accordingly.