Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python-package] take shallow copy of dataframe in predict (fixes #6195) #6218

Merged
merged 5 commits into from
Dec 7, 2023

Conversation

jmoralez
Copy link
Collaborator

Takes a shallow copy of the dataframe in the _data_from_pandas function to avoid modifying it when converting categorical features to their codes. This used to always be done (implicitly) for predict until #6066, since the function is called with feature_name='auto' and the copy argument of pandas.DataFrame.rename is passed as the deep argument to pandas.DataFrame.copy, however that behavior wasn't obvious.

Fixes #6195.

Also updates the test that verifies the original dataframe isn't modified to now consider the case of unseen categories, which fails in the current master and should prevent #6195 from happening again.

@jameslamb jameslamb mentioned this pull request Dec 1, 2023
24 tasks
Copy link
Collaborator

@jameslamb jameslamb left a comment

Choose a reason for hiding this comment

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

Looks great, thank you! Sorry for breaking this 😅

And excellent report in #6195 @fingoldo, thank you very much for your effort there.

@jmoralez
Copy link
Collaborator Author

jmoralez commented Dec 6, 2023

Haha no worries. I missed it too when reviewing, the predict path wasn't clear (feature_name='auto' leading to the shallow copy).

@jameslamb jameslamb merged commit e797985 into master Dec 7, 2023
41 checks passed
@jameslamb jameslamb deleted the shallow-copy-predict branch December 7, 2023 14:54
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.

[python-package] LightGBM predict_proba() corrupts pandas categorical columns with unseen values
3 participants