Repository navigation
Closed
Labels
Description
Describe the bug and give evidence about its user-facing impact
The logic for array API was recently modified to allow 'y' inputs of a potentially different type than 'X', particularly when it comes to classifiers where 'y' might be strings.
But it appears this logic does not always work when y is a pandas object, or at least doesn't work for all methods.
I guess an easy fix would be to convert pandas 'y' to numpy inside move_to, but not sure if by that point it is always passed as an integer-encoded array for classifiers.
Steps/Code to Reproduce
import os
os.environ["SCIPY_ARRAY_API"] = "1"
import numpy as np
import pandas as pd
import torch
from sklearn.linear_model import RidgeClassifier
from sklearn import config_context
rng = np.random.default_rng(seed=123)
X = rng.standard_normal(size=(100,10))
y = rng.integers(2, size=X.shape[0])
Xt = torch.tensor(X)
ys = pd.Series(y)
with config_context(array_api_dispatch=True):
model = RidgeClassifier(solver="svd").fit(Xt, ys)
score = model.score(Xt, ys)Expected Results
No error.
Actual Results
Traceback (most recent call last):
Cell In[3], line 18
score = model.score(Xt, ys)
File ~/code/scikit-learn/sklearn/base.py:615 in score
return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
File ~/code/scikit-learn/sklearn/utils/_param_validation.py:218 in wrapper
return func(*args, **kwargs)
File ~/code/scikit-learn/sklearn/metrics/_classification.py:407 in accuracy_score
y_true, sample_weight = move_to(y_true, sample_weight, xp=xp, device=device)
File ~/code/scikit-learn/sklearn/utils/_array_api.py:602 in move_to
array_converted = xp.from_dlpack(array, device=device)
File ~/venvs/uv-xpu-dpnp/lib/python3.13/site-packages/torch/utils/dlpack.py:226 in from_dlpack
raise AssertionError(
AssertionError: device and copy kwargs not supported when ext_tensor is already a DLPack capsuleVersions
System:
python: 3.13.12 | packaged by conda-forge | (main, Feb 5 2026, 05:53:46) [GCC 14.3.0]
executable: /export/users/dcortes/miniforge3/envs/torch19/bin/python
machine: Linux-6.8.0-49-generic-x86_64-with-glibc2.39
Python dependencies:
sklearn: 1.9.dev0
pip: 26.0.1
setuptools: 82.0.0
numpy: 2.4.2
scipy: 1.17.0
Cython: 3.2.4
pandas: 3.0.0
matplotlib: 3.10.8
joblib: 1.5.3
threadpoolctl: 3.6.0
Built with OpenMP: True
threadpoolctl info:
user_api: blas
internal_api: openblas
num_threads: 128
prefix: libopenblas
filepath: /localdisk2/mkl/dcortes/miniforge3/envs/torch19/lib/libopenblasp-r0.3.30.so
version: 0.3.30
threading_layer: pthreads
architecture: SapphireRapids
user_api: openmp
internal_api: openmp
num_threads: 112
prefix: libgomp
filepath: /localdisk2/mkl/dcortes/miniforge3/envs/torch19/lib/libgomp.so.1.0.0
version: NoneInterest in fixing the bug
No.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
virchan commentedon Apr 22, 2026
Thank you for the report, @david-cortes-intel.scikit-learn’s array API support is limited to NumPy, CuPy, and PyTorch. Since Pandas is not one of them, the behaviour you observed is expected.
Closing as not a bug.Edit: See #33822 (comment).
lorentzenchr commentedon Apr 22, 2026
@virchan Don‘t you find the error message cryptic?
david-cortes-intel commentedon Apr 22, 2026
@virchan Thanks for your quick response. This detail is very unclear from the documentation for array API, and it's not obvious to me from what one can find throughout this issue tracker that it is the intended design either (e.g. #32836 #28668 (comment))
Would be very helpful if the docs could describe in more details these compatibility constraints.
virchan commentedon Apr 22, 2026
I don't, but that's probably because I've become numb after working on Array API support for a while and, as a result, overlooked the user's point of view 😅.
But I see your point now, @david-cortes-intel. The
move_tofunction should be able to move Pandas dataframe to the reference array type, since we consider it as NumPy compat namespace in #32838.Reopening the issue, and apologies for the noise.
betatim commentedon Apr 28, 2026
I'm not sure if we should start accepting
pd.Seriesand converting it for the user. The reason I am a bit hesitant is that once we start allowing it, we can't easily go back to not accepting it. This is what makes me want to ponder this for a bit.For example, I think we should not get in a situation where it looks like
move_to(X_df, ..)would work. I think whenXis a data frame we should take the "not array API" path through the scikit-learn code and not trigger a conversion of it to Numpy.The reason I bring up moving
Xis that it feels like that is an edge case we should consider in the decision of how all this should work.A reason to support converting a
y = pd.Series()for the user is that I think you can passy = list()and we convert that too. Even though alistisn't a array API thing.We should update the documentation and improve the error messages. No matter if we decide to accept
pd.Seriesthere will always be things that we do not accept, so having a good error message seems like a good thing.ogrisel commentedon Apr 30, 2026
I think we can/should accept
pd.Serieswhen it has a numerical dtype (but fail otherwise, as for a numpy array with str or object dtype).3 remaining items
ogrisel commentedon May 18, 2026
Indeed, there is a problem in the way we do things. I think we should not try to move
y_truewhen it holds non-numeric content in classification metrics. Instead, we should move the result of label encoding / one-hot / ordinal encoding of the class labels into boolean or integer class membership indicators just before they are combined withy_predandsample_weightusing the namespace / device / dtype ofy_pred. The details of how this happens is specific for each classification metric I believe.ogrisel commentedon May 20, 2026
Note that there is a related fix in #34065 to make
RidgeClassifier.fitpass the common tests for mixed input namespaces, but that does not fix the problem with.score. We probably need to improve the common test to cover this.StefanieSenger commentedon May 23, 2026
I can see how from a user's perspective, one might expect a pandas series or df to comply to the array API rules, since we did not document otherwise and pandas input is usually converted to numpy in the process (in
check_array). Thanks for reporting that, @david-cortes-intel.But in array API we are explicitly excluding pandas from being an array namespace. In
_remove_non_arrays(called inget_namespace_and_device), we filter out dataframe inputs:scikit-learn/sklearn/utils/_array_api.py
Lines 350 to 351 in 0f0ff8c
Therefore, and because we are planning to support narwhals, I think it is better not to take pandas objects under the umbrella of
array_api_dispatch=Trueright now (which in my mind would be a new feature).I would suggest two actions:
CC, @AnneBeyer, because this issue is a good starting point for thinking about the scope of array API, if you are interested to participate.
ogrisel commentedon May 28, 2026
I find it unexpected that
model.score(X, y)would accept y as a pandas series whenarray_api_dispatch=Falsebut would raise an exception whenarray_api_dispatch=True.The goal is to enable array API dispatch by default in the future, we should not introduce a usability regression when doing so.
I think we need to find a way to change to ensure that
model.fit(X, y)andmodel.score(X, y)still accept pandas dataframes and series whether array API dispatch is enabled or not.Since when array API dispatch is disabled, this particular estimator treats pandas series as array-like and converts them to NumPy array, we should continue doing so here.
Other estimators such as
ColumnTransformerhave particular handling of pandas dataframes, and we should ensure that we do not break that.ogrisel commentedon May 28, 2026
I think this is a bug to me, and we probably need to extend the common test discussed in #34094 to cover non-array API array-like inputs (in particular dataframes and series).