Skip to content

Adding enable_categorical to .apply method #11550

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

Merged
merged 4 commits into from
Jul 10, 2025

Conversation

imeyer2
Copy link
Contributor

@imeyer2 imeyer2 commented Jul 9, 2025

Currently, XGBModel can be trained with enable_categorical=True when the input data fields have categorical type when appropriate. However, the .apply method will throw when trying to retrieve leaf values.

Example

import xgboost as xgb
import numpy as np
import pandas as pd

model = xgb.XGBClassifier(enable_categorical = True)

arr = np.random.rand(5,5)

df = pd.DataFrame(arr)

df['test']= ['one', 'two','three', 'four', 'five']


df = df.convert_dtypes()

# convert string to categorical
df['test'] = df['test'].astype('category')

model.fit(df, [0,1,2,3,4]) # works


model.apply(df) # fails

Produces

Exception has occurred: ValueError
DataFrame.dtypes for data must be int, float, bool or category. When categorical type is supplied, the experimental DMatrix parameter`enable_categorical` must be set to `True`.  Invalid columns:test: category
KeyError: 'category'

During handling of the above exception, another exception occurred:

  File "/path/to/python/file/test.py", line 22, in <module>
    model.apply(df)
ValueError: DataFrame.dtypes for data must be int, float, bool or category. When categorical type is supplied, the experimental DMatrix parameter`enable_categorical` must be set to `True`.  Invalid columns:test: category

Copy link
Member

@trivialfis trivialfis left a comment

Choose a reason for hiding this comment

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

Thank you for the fix! Could you please help add a small test in the test_with_sklearn.py file?

Added test for .apply
@imeyer2
Copy link
Contributor Author

imeyer2 commented Jul 10, 2025

Thank you for the fix! Could you please help add a small test in the test_with_sklearn.py file?

Done!

@trivialfis trivialfis merged commit 9c0efce into dmlc:master Jul 10, 2025
62 checks passed
@trivialfis trivialfis mentioned this pull request Jul 19, 2025
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants