Skip to content
ML Academy

Classical ML, validation, metrics, interpretation, debugging · insight-1170

Check how pandas ordered categoricals reach LightGBM before assuming they are split as categories

Evidence type
documented fact
Difficulty
medium
Roles
Data scientist, ML engineer
Industries
retail, banking

Summary: situation, action, reason, boundary

  1. Situation

    A column typed as an ordered pandas categorical (fare class, risk grade) goes into LightGBM with categorical_feature left at its default.

  2. Action

    Decide per column. Keep it ordered when the order is real and thresholds are what you want, or name it in categorical_feature to force categorical splits. Pass the list explicitly, and keep the category list with the model so codes cannot be reassigned.

  3. Reason

    Auto-detection covers only unordered pandas categoricals; the library converts category values to integer codes aligned with the training categories.

  4. Boundary

    Pipelines that build dtypes with pandas and pass DataFrames straight to LightGBM.

Symptom or risk

Ordered categoricals are treated as ordinal numeric features, so only threshold splits on the code are available; a non-ordinal pattern is missed, and an unordered cast of the same column behaves differently.

Exceptions

If the data are truly ordinal, the numeric treatment is the intended feature, not a bug.

Trade-offs

Native categorical splits fit more patterns but can overfit when levels are many and small.

Pitfalls

Building integer codes with your own encoder and refitting it at serving time, which reorders codes without any error.

Lessons and concepts

Claims and their sources