How do Random Forests handle missing values and categorical features in practice?

Last updated 5 November 2026

  • Missing values: scikit-learn ≥ 1.4 handles NaN natively by learning which child missing values go to. R uses proximity-based imputation (rfImpute). A common default is SimpleImputer plus a missing-indicator column.

  • Categoricals: scikit-learn needs encoding. Ordinal encoding works well for trees, one-hot suits low cardinality, and out-of-fold target encoding suits high cardinality. R and H2O split on category subsets natively.

  • Fit imputers and encoders inside a Pipeline to avoid CV leakage, and handle unseen categories with handle_unknown.

  • https://scikit-learn.org/stable/auto_examples/release_highlights/plot_release_highlights_1_4_0.html
  • https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html
  • https://scikit-learn.org/stable/modules/impute.html
All Random Forests interview questions