Skip to content

TargetEncoder.fit_transform raises KeyError: 'groups' when metadata routing is enabled and no groups are passed #35139

Closed

Description

@lepennec

Introduce yourself

Dear all,
I am a professor of applied math at École polytechnique, very close to the Turing build, so important for the scikit-learn community. I use scikit-learn mainly for teaching, as the ones doing the hard jobs are now my students.

Describe the bug and give evidence about its user-facing impact

With enable_metadata_routing=True, TargetEncoder.fit_transform(X, y) raises KeyError: 'groups' when it is called without groups. This is the common case. It also happens inside a ColumnTransformer or a Pipeline, so any pipeline that contains a TargetEncoder fails as soon as routing is switched on globally. The bug does not depend on the CV splitter: the default cv=5 and StratifiedKFold(...) both fail.

The cause is in sklearn/preprocessing/_target_encoder.py (line 366 in 1.9.1, unchanged on main):

if _routing_enabled():
    if params["groups"] is not None:
        X, y, params["groups"] = indexable(X, y, params["groups"])
    routed_params = process_routing(self, "fit_transform", **params)

params only contains the metadata that were actually passed, so params["groups"] fails when no groups are given.

Steps/Code to Reproduce

import numpy as np
import sklearn
from sklearn.preprocessing import TargetEncoder

sklearn.set_config(enable_metadata_routing=True)

rng = np.random.default_rng(0)
X = rng.choice(["a", "b", "c"], size=(100, 1))
y = rng.integers(0, 2, size=100)

TargetEncoder().fit_transform(X, y)

Expected Results

No error. The cross-fitted encoding should be the same as with routing disabled, since there is no metadata to route.

Actual Results

  File ".../sklearn/preprocessing/_target_encoder.py", line 366, in fit_transform
    if params["groups"] is not None:
       ~~~~~~^^^^^^^^^^
KeyError: 'groups'

Versions

System:
    python: 3.12.14 (main, Sep  1 2026, 14:17:39) [MSC v.1944 64 bit (AMD64)]

Python dependencies:
      sklearn: 1.9.1
        numpy: 2.5.4
        scipy: 1.18.1
       pandas: 3.0.6
       joblib: 1.6.0
threadpoolctl: 3.7.0
     narwhals: 2.27.1
``

Interest in fixing the bug

`Fixing the bug appears to be easy:
Replacing

           if params["groups"] is not None:

by

            if params.get("groups") is not None:

in File ".../sklearn/preprocessing/_target_encoder.py", line 366.

I will be pleased to help fix this bug, which will be my first commit to scikit-learn.

Activity

  1. added
    Needs Triage
    on Oct 13, 2026
  2. added
    Needs Investigation
    and removed
    Needs Triage
    on Oct 13, 2026
  3. lesteve commented on Oct 13, 2026

    @lesteve
    Member

    Thanks for the clear issue and your interest in fixing the bug, really appreciate it 🙏!

    It does seem like a bug indeed, ccing @StefanieSenger who will have a more informed opinion than me about the fix you propose.

  4. added
    Metadata Routing
    and removed
    Needs Investigation
    on Oct 13, 2026
  5. StefanieSenger commented on Oct 14, 2026

    @StefanieSenger
    Member

    Thanks for reporting the issue, @lepennec. I confirm this is a bug and needs fixing.

    I'll be happy to review your bugfix PR. (Feel free to ping me on it, so I cannot miss it.) Your suggested solution looks fine. Please also add a changelog entry and a non-regression test (reusing your reproducible for this will suffice).

    No pressure, but FYI, if we manage to merge this by November 2nd it will go into next release.

  6. faizanfatmi commented on Oct 15, 2026

    @faizanfatmi
  7. faizanfatmi commented on Oct 15, 2026

    @faizanfatmi
  8. lesteve commented on Oct 15, 2026

    @lesteve
    Member

    @faizanfatmi the original poster said he was interested to work on this 2 days ago, I suggest you find another issue to work on.

    Glad to see you are interested to contribute to scikit-learn 🙏!

    Please have a look at our contributing doc and in particular the section Issues for new contributors.

    There is also GitHub's How to contribute to Open-Source guide, which gives a good generic overview about how open-source works.

  9. StefanieSenger commented on Oct 20, 2026

    @StefanieSenger
    Member

    No pressure, but FYI, if we manage to merge this by November 2nd it will go into next release.

    Adding to this 😅: now that we have #35144 that @ogrisel suggested to have in the next release, we really need to ship this bugfix as well. Did you have any time to work on this @lepennec? You really already have everything you need: the fix is the one-liner you suggested and the test is a simple smoke test derived from your reproducible. You can also make a start and we finish.

  10. lepennec commented on Oct 20, 2026

    @lepennec
    ContributorAuthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Metadata Routing

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions