XGBoost AttributeError 'DataFrame' object has no attribute 'feature_names'
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
This error appears when code treats a pandas DataFrame like an XGBoost data container. A pandas DataFrame stores column labels in .columns, while feature_names is an attribute associated with XGBoost structures such as DMatrix.
Why The Error Happens
The failing pattern usually looks like this:
That raises:
The reason is simple: pandas does not define a feature_names property on DataFrames. The feature labels are available through df.columns.
Use .columns When The Data Is Still A DataFrame
If all you need is the list of model input names, use:
That is the correct pandas-side answer.
This matters because many examples mix pandas preprocessing with low-level XGBoost training, and it becomes easy to blur the line between the two libraries.
Use DMatrix Correctly In Low-Level XGBoost Code
If you are calling xgboost.train, create a DMatrix and pass the feature names there if needed:
In that workflow, feature_names belongs to the XGBoost matrix, not to the original DataFrame object.
The Scikit-Learn Wrapper Is Often Easier
If you are not using advanced DMatrix features, the scikit-learn style API is usually simpler:
This removes a lot of manual plumbing and reduces the chance of mixing pandas and DMatrix concepts incorrectly.
Preserve Feature Names Through The Pipeline
A related source of confusion is converting the DataFrame to a NumPy array too early:
That may be fine numerically, but the column labels are now separate from the data. If you later need named features for debugging or importance reporting, you must pass the names yourself.
Keeping the data as a DataFrame longer often makes debugging easier because the schema remains attached to the values.
In production systems, it is often worth storing the expected training columns and validating them before prediction. A quick schema check can catch renamed, missing, or reordered fields before the request reaches the model layer. That is much easier to diagnose than a vague scoring discrepancy after the model has already consumed bad input.
Common Pitfalls
One common mistake is assuming all machine-learning libraries expose feature names through the same attribute. They do not.
Another issue is mixing the xgboost.train API with examples written for the scikit-learn wrapper, or the other way around.
A third problem is losing the feature labels by converting to NumPy too early and then wondering why later objects cannot describe the columns.
Finally, even when feature names are present, column order still matters. A named schema does not protect you from passing the wrong feature arrangement at prediction time.
Summary
- A pandas
DataFrameuses.columns, not.feature_names. - '
feature_namesis relevant to XGBoost data structures such asDMatrix.' - Use
df.columns.tolist()when you need names from a DataFrame. - Prefer the scikit-learn XGBoost wrapper when you do not need low-level
DMatrixcontrol. - Keep feature names and column order consistent throughout training and inference.
Related reading
- xgboost AttributeError 'DMatrix' object has no attribute 'handle
- xgboost binary logistic regression
- XGboost cannot pass validation data for eval_set in pipeline
- XGBoost error - When categorical type is supplied, DMatrix parameter enable_categorical must be set to True
- XGBoost for multilabel classification?
- xgboost in R how does xgb.cv pass the optimal parameters into xgb.train
- xlrd.biffh.XLRDError Excel xlsx file; not supported
- You are trying to add a non-nullable field 'new_field' to userprofile without a default
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.