I would like to use cross validation with catboost. Since I do not just want to use catboost but also sampling I am using a pipeline and hence cannot use catboost's own cross validation (which works if I just use catboost and not a pipeline). So I want to use sklearn's cross validation, which works fine if I use just numerical variables but as soon as I also include the categorical variables (cat_features) and use catboost's encoding, cross_validate doesn't work anymore. Even if I don't use a pipeline but just catboost alone I get a KeyError: 0 message with cross_validate. But I don't understand why. This is part of my code that doesn't work:

from sklearn.model_selection import cross_validate
model = cb.CatBoostClassifier(**params, cat_features=cat_features)
cv_score = cross_validate(model, X_train, y_train, scoring='roc_auc', cv=5, return_train_score=True)
  • $\begingroup$ Could you identify where in the error traceback the KeyError is happening? Are your cat_features strings/column names, or integers/column indices? $\endgroup$
    – Ben Reiniger
    Aug 26, 2019 at 2:58
  • $\begingroup$ My cat_features are object and category types. The KeyError happens already in the beginning, the error message starts like this: KeyError Traceback (most recent call last) ~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 2656 try: -> 2657 return self._engine.get_loc(key) 2658 except KeyError: pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() .... KeyError: 0 During handling of the above exception, another exception occurred: $\endgroup$ Aug 26, 2019 at 9:39
  • $\begingroup$ I think it is because sklearn's cross_validate cannot really encode the categorical features while fitting the data. It's probably a problem of sklearn's cross_validate as everything works perfectly fine when doing the same thing but using model.fit. $\endgroup$ Aug 27, 2019 at 11:55
  • 1
    $\begingroup$ I can't recreate the error. See github.com/bmreiniger/datascience.stackexchange/blob/master/… . Can you share your code, data, and/or full traceback? $\endgroup$
    – Ben Reiniger
    Aug 27, 2019 at 14:26
  • 1
    $\begingroup$ I found out when i do xtrain.astype('O') it works. $\endgroup$ Aug 28, 2019 at 13:11

1 Answer 1


I found out that adding xtrain.astype('O') works.

Apparently catboost doesn't work with pandas Categorical dtypes yet: https://github.com/catboost/catboost/issues/814


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.