Repository navigation
Closed
Description
Describe the bug and give evidence about its user-facing impact
In scikit-learn 1.8.0,r2_score() and other metric functions supported pyarrow.ChunkedArray input.
With the latest nightlies, they raise a hard-to-understand error from inside narwhals.
I suspect this was broken by #31127. Reporting because this showed up in lightgbm's tests using the latest scikit-learn nightlies (lightgbm-org/LightGBM#7269).
Steps/Code to Reproduce
Using Python 3.14 just as an example, I don't have any reason to suspect this is sensitive to Python version.
docker run \
--rm \
-it python:3.14 \
bash
pip install \
--prefer-binary \
--extra-index-url https://pypi.anaconda.org/scientific-python-nightly-wheels/simple \
'cffi>=2.0.0' \
'numpy>=2.5.0.dev0' \
'pandas>=3.1.0.dev0' \
'pyarrow>=24.0.0.dev0' \
'scikit-learn>=1.9.dev0' \
'scipy>=1.18.0.dev0'
cat > test.py <<EOF
import pandas as pd
from pyarrow import chunked_array
from sklearn.metrics import r2_score
y_true = pd.Series([0.1, 0.2, 0.3])
y_pred = chunked_array(y_true)
print(f"R2: {r2_score(y_true, y_pred)}")
EOF
# this fails (see "Actual Results")
python ./test.py
Installing the latest scikit-learn release (1.8.0) into the environment and leaving everything else the same, the example passes.
$ pip install --no-deps 'scikit-learn==1.8.0'
$ python ./test.py
R2: 1.0
Expected Results
r2_score() (and other metric functions) to accept pyarrow.ChunkedArray inputs and process them correctly.
Actual Results
Traceback (most recent call last):
File "//./test.py", line 7, in <module>
r2_score(y_true, y_pred)
~~~~~~~~^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.14/site-packages/sklearn/utils/_param_validation.py", line 218, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.14/site-packages/sklearn/metrics/_regression.py", line 1288, in r2_score
_check_reg_targets_with_floating_dtype(
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
y_true, y_pred, sample_weight, multioutput, xp=xp
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/usr/local/lib/python3.14/site-packages/sklearn/metrics/_regression.py", line 209, in _check_reg_targets_with_floating_dtype
y_type, y_true, y_pred, sample_weight, multioutput = _check_reg_targets(
~~~~~~~~~~~~~~~~~~^
y_true, y_pred, sample_weight, multioutput, dtype=dtype_name, xp=xp
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/usr/local/lib/python3.14/site-packages/sklearn/metrics/_regression.py", line 114, in _check_reg_targets
check_consistent_length(y_true, y_pred, sample_weight)
~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.14/site-packages/sklearn/utils/validation.py", line 453, in check_consistent_length
lengths = [_num_samples(X) for X in arrays if X is not None]
~~~~~~~~~~~~^^^
File "/usr/local/lib/python3.14/site-packages/sklearn/utils/validation.py", line 381, in _num_samples
return nw.from_native(x).shape[0]
~~~~~~~~~~~~~~^^^
File "/usr/local/lib/python3.14/site-packages/narwhals/stable/v2/__init__.py", line 484, in from_native
return _from_native_impl( # type: ignore[no-any-return]
native_object,
...<5 lines>...
version=Version.V2,
)
File "/usr/local/lib/python3.14/site-packages/narwhals/translate.py", line 383, in _from_native_impl
raise TypeError(msg)
TypeError: Please set `allow_series=True` or `series_only=True`
Versions
output of 'pip list' (click me)
$ pip list
Package Version
--------------- --------------------------
cffi 2.0.0
joblib 1.5.3
narwhals 2.21.0
numpy 2.5.0.dev0
pandas 3.1.0.dev0+893.g9187363089
pip 26.1.1
pyarrow 25.0.0.dev48
pycparser 3.0
python-dateutil 2.9.0.post0
scikit-learn 1.9.dev0
scipy 1.18.0.dev0
six 1.17.0
threadpoolctl 3.6.0
Interest in fixing the bug
Not available to put up a PR in the near future, sorry.
Reacted by Marco Edward Gorelli
Activity
[-]r2_score no longer supports `pyarrow.ChunkedArray` inputs[/-][+]r2_score() no longer supports `pyarrow.ChunkedArray` inputs[/+]Ic3fr0g commentedon May 12, 2026
jeremiedbb commentedon May 12, 2026
Thanks for the report. Ping @lorentzenchr
Ic3fr0g commentedon May 12, 2026
I would like to help @jeremiedbb , let me know if you'd like to see a fix for this?