Skip to content

[docs] [python-package] Update pandas categorical feature encoding details - #7250

Merged
jameslamb merged 4 commits into
lightgbm-org:masterfrom
maxzw:patch-1
Jun 2, 2026
Merged

jameslamb merged 4 commits into
lightgbm-org:masterfrom
maxzw:patch-1

Conversation

@maxzw

@maxzw maxzw commented May 1, 2026 •

Copy link
Copy Markdown
Contributor

The current documentation states

integer codes will be extracted from pandas categoricals in the Python-package

However, this statement alone leaves out a very important nuance: if the category mapping observed using during prediction differs from the mapping observed during training, the mapping of the training data is applied to the prediction dataset. This is important information, because this means that categorical encodings are part of the learning process of LightGBM and no external data processing is required to prevent mixing up categorical codes (e.g. OrdinalEncoder).

Related to this discussion: #2761

The application of training categories to prediction data is done here:

for col, category in zip(cat_cols, pandas_categorical):
if list(data[col].cat.categories) != list(category):
data[col] = data[col].cat.set_categories(category)

Clarified encoding requirements for categorical features and added a note about category handling in LightGBM.

Related lightgbm-org#2761 (comment)
@jameslamb jameslamb added the doc label May 1, 2026
@jameslamb jameslamb changed the title Update pandas categorical feature encoding details [Python] [docs] [python-package] Update pandas categorical feature encoding details May 1, 2026

@jameslamb jameslamb 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.

Thanks for your interest in LightGBM. I agree that this is a gap in the documentation, and appreciate you taking the time to open a PR! I've left some suggestions.

The application of training categories to prediction data is done here: https://github.com/henry0312/LightGBM/blob/ad487fee57945f45041ff21038dfeaaee4e10e52/python-package/lightgbm/basic.py#L684

How did you end up using a many-years-out-of-date old maintainer's fork for this link? I'm genuinely curious, I was really surprised to see that link.

In this repo, here in 2026, I think the relevant code snippet would be:

for col, category in zip(cat_cols, pandas_categorical):
if list(data[col].cat.categories) != list(category):
data[col] = data[col].cat.set_categories(category)

Note that posting a link from within the same repo has the nice side effect of the code being rendered in the GitHub UI, so it's visible without clicking through.

Comment thread docs/Advanced-Topics.rst Resolved
Comment thread docs/Advanced-Topics.rst Resolved
@maxzw

maxzw commented May 2, 2026

Copy link
Copy Markdown
Contributor Author

How did you end up using a many-years-out-of-date old maintainer's fork for this link? I'm genuinely curious, I was really surprised to see that link.

I tried to trace back my steps but honestly I have no idea πŸ˜„ Good to know about the rendering!

I've updated this branch with your comments, curious to see what you think.

@jameslamb jameslamb 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.

Changes look good, thanks very much!

Don't worry about the failing CI jobs, we have some ongoing issues right now (the global Ubuntu packages outage and #7248). I'll get this PR updated and merged once those are resolved, nothing else you need to do for this one.

@jameslamb
jameslamb merged commit 1c0970b into lightgbm-org:master Jun 2, 2026
59 checks passed
@jameslamb

Copy link
Copy Markdown
Member

Thanks @maxzw , and sorry it took so long to merge this!

Will try to get your other PRs moving as soon as we land #7275 (just waiting on reviews there). Are there other areas not related to array processing you're interested in? I'd be happy to suggest some other places where LightGBM could use help.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants