Skip to content

MNT use narwhals in indexable - #33959

Merged
jeremiedbb merged 5 commits into
scikit-learn:mainfrom
lorentzenchr:nw_indexable
Jul 28, 2026
Merged

jeremiedbb merged 5 commits into
scikit-learn:mainfrom
lorentzenchr:nw_indexable

Conversation

@lorentzenchr

Copy link
Copy Markdown
Member

Reference Issues/PRs

Contributes to #33958.

What does this implement/fix? Explain your changes.

Uses narwhals in indexable instead of iloc.

AI usage disclosure

None

Any other comments?

@FrancoisPgm FrancoisPgm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

Comment thread sklearn/utils/validation.py Resolved
Comment thread sklearn/utils/validation.py Outdated
Comment on lines +480 to +481
elif _nw_is_into_df_or_series(iterable):
return nw.from_native(iterable)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the tests use dataframes (polars, pandas, pyarrow) that have getitem.

  • if we want to support other dataframes we should test them
  • otherwise we should not include this line

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the order, dataframes are converted to narwhals before getitem is checked because getitem has very different semantics for different dataframes.

@lorentzenchr

Copy link
Copy Markdown
Member Author

#34051 needs to be merged first.

@lorentzenchr

Copy link
Copy Markdown
Member Author

@jeremiedbb Wanna merge?

Comment on lines +842 to +843
def check_df(x):
return isinstance(x, input_type) or isinstance(x.to_native(), input_type)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So now we're going to have narwhals dataframes moving around in fit, which is fine. My concern is: are we sure that it does not surface to the user ? I believe we're safe in sklearn since we never return dataframes outside of set_output (do we ?). But 3rd party estimators might. I know you're going to argue that _make_indexable is private so we don't care, but still 😄.

@lorentzenchr lorentzenchr Jun 12, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a convention what is public and what is private. If I can‘t rely on that convention as a developer, I‘ll stop contributing.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeremiedbb Is this a blocker?
@adrinjalali once asked here

do you think you could drive this?

I said yes, but it needs community support. If we want to profit from narwhals, internal things will need to change.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the "exposed to user" worry is reasonable, but pretty easy to handle:

  • make sure we have a common test which checks for the output type, given different input types (I think we already have this?)
  • if we want to really care about the return types of existing private methods, we can create a new method anywhere we want to do this kind of change. For instance, here we'd do _make_nw_indexable instead of changing existing one, and deprecate the old one. I think it's dirty, unnecessary, and adds avoidable maintenance burden, but doable. I'd be more in favor of not doing this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @adrinjalali, for your input.

Expectations management: As a developer, I want to rely on our convention and not implement a new private API (_make_nw_indexable). I won't implement that, others can do this.

Personal impression: It feels like paranoia (and a waste of my time).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeremiedbb could we move this forward?

@jeremiedbb
jeremiedbb enabled auto-merge (squash) July 28, 2026 09:30
@jeremiedbb
jeremiedbb merged commit 925873e into scikit-learn:main Jul 28, 2026
31 checks passed
@lorentzenchr

Copy link
Copy Markdown
Member Author

@jeremiedbb THX 🙏

@lesteve

lesteve commented Jul 29, 2026

Copy link
Copy Markdown
Member

Looks like this broke the doc build see build log for more details and error below.

Remember to use [doc build] in your commit message to make sure the PR is fixing the issue.

    Unexpected failing examples (1):
    
        ../examples/compose/plot_column_transformer_mixed_types.py failed leaving traceback:
    
        Traceback (most recent call last):
          File "/home/circleci/project/examples/compose/plot_column_transformer_mixed_types.py", line 198, in <module>
            search_cv.fit(X_train, y_train)
            ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
          File "/home/circleci/project/sklearn/base.py", line 1403, in wrapper
            return fit_method(estimator, *args, **kwargs)
          File "/home/circleci/project/sklearn/model_selection/_search.py", line 1184, in fit
            self.best_estimator_.fit(X, y, **routed_params.estimator.fit)
            ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          File "/home/circleci/project/sklearn/base.py", line 1403, in wrapper
            return fit_method(estimator, *args, **kwargs)
          File "/home/circleci/project/sklearn/pipeline.py", line 643, in fit
            Xt = self._fit(
                X, y, routed_params, raw_params=params, callback_ctx=callback_ctx
            )
          File "/home/circleci/project/sklearn/pipeline.py", line 568, in _fit
            X, fitted_transformer = fit_transform_one_cached(
                                    ~~~~~~~~~~~~~~~~~~~~~~~~^
                cloned_transformer,
                ^^^^^^^^^^^^^^^^^^^
            ...<7 lines>...
                callback_ctx=subcontext,
                ^^^^^^^^^^^^^^^^^^^^^^^^
            )
            ^
          File "/home/circleci/miniforge3/envs/testenv/lib/python3.14/site-packages/joblib/memory.py", line 326, in __call__
            return self.func(*args, **kwargs)
                   ~~~~~~~~~^^^^^^^^^^^^^^^^^
          File "/home/circleci/project/sklearn/pipeline.py", line 1609, in _fit_transform_one_with_callbacks
            Xt, transformer = _fit_transform_one(
                              ~~~~~~~~~~~~~~~~~~^
                transformer, X, y, weight, message_clsname, message, params
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            )
            ^
          File "/home/circleci/project/sklearn/pipeline.py", line 1582, in _fit_transform_one
            res = transformer.fit_transform(X, y, **params.get("fit_transform", {}))
          File "/home/circleci/project/sklearn/utils/_set_output.py", line 321, in wrapped
            data_to_wrap = f(self, X, *args, **kwargs)
          File "/home/circleci/project/sklearn/base.py", line 1403, in wrapper
            return fit_method(estimator, *args, **kwargs)
          File "/home/circleci/project/sklearn/compose/_column_transformer.py", line 969, in fit_transform
            self._validate_column_callables(X)
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
          File "/home/circleci/project/sklearn/compose/_column_transformer.py", line 521, in _validate_column_callables
            columns = columns(X)
          File "/home/circleci/project/sklearn/compose/_column_transformer.py", line 1502, in __call__
            raise ValueError(
                "make_column_selector can only be applied to pandas dataframes"
            )
        ValueError: make_column_selector can only be applied to pandas dataframes
    
    -------------------------------------------------------------------------------

@lorentzenchr

Copy link
Copy Markdown
Member Author

@lesteve Thanks for the report. I think our tests are not good enough. It should not require a doc build.

I can try to fix it in the coming days.

@lorentzenchr
lorentzenchr deleted the nw_indexable branch July 30, 2026 16:34
prady0t pushed a commit to prady0t/scikit-learn that referenced this pull request Sep 2, 2026
@jeremiedbb jeremiedbb mentioned this pull request Sep 8, 2026
14 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants