[python] Iterate pandas columns with items() instead of a name lookup per column #12815
All Vouch review confidence checks passed
Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 433 lines diff across 5 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.363
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #12815"
general
weight: 0.35
RAMitchell
What
Two changes to the pandas input path in
python-package/xgboost/data.py:pandas_feature_infobuilds the feature names with[str(col) for col in data.columns]instead oflist(data.columns.map(str)).pandas_transform_dataloops overdata.items()instead of looking up every column by name withdata[col].The outputs are the same: the same feature names, the same arrays, and the same predictions.
Why
For small batches, most of the time in
predict/predict_probaon a DataFrame goes to converting the frame, not to the model. In the benchmark below, with 1 row and 42 columns on macOS,_transform_pandas_dftakes 489 us of a 594 uspredict_probacall. Two parts of that are pure overhead:Index.map(str)builds a new pandas Index just to turn it into a list. With pandas 3, string column labels are stored in an Arrow-backed Index, and the map took 49 us where the list comprehension takes 11 us.data[col]hashes the label and looks it up for every column, whiledata.items()walks the columns by position. For 42 columns that is 171 us vs 141 us.How to test
Existing tests cover feature names from int,
range, string and MultiIndex columns, plus every column dtype the loop handles.tests/python/test_with_pandas.pygives 20 passed.Benchmark:
XGBClassifier(n_estimators=50, max_depth=6, enable_categorical=True, n_jobs=4)trained on 20,000 rows with 42 columns: 40 float, 1 category, 1 int. I timedpredict_probaon the first 1, 100 and 10,000 rows, 1000 calls each (50 for 10,000 rows), and took the median. I ran 3 rounds alternatingmasterand this PR, using the same libxgboost build for both, and report the median round. In every run, both builds gave the same hash of the predictions and the same feature names and types.Environments
| | macOS | Linux |
|---|---|---|
| Machine | Apple M4 Pro | same Mac, Docker 29.7.2 (4 CPUs, 6 GB) |
| OS | macOS 26.5.1 | Debian 13 (
python:3.12), aarch64 || Python | 3.12.13 | 3.12.16 |
| Libraries | numpy 2.5.4, scikit-learn 1.9.1, pandas 3.1.0, pyarrow 26.0.1 | numpy 2.5.4, scikit-learn 1.9.1; pandas 3.1.0 with pyarrow 26.0.1, and pandas 2.3.3 |
predict_probatime per call| Rows | macOS, pandas 3.1 | Linux, pandas 3.1 | Linux, pandas 2.3 |
|---:|---:|---:|---:|
| 1 | 597 → 510 us (−15%) | 755 → 645 us (−15%) | 436 → 379 us (−13%) |
| 100 | 624 → 528 us (−15%) | 890 → 761 us (−14%) | 547 → 509 us (−7%) |
| 10,000 | 1,160 → 1,173 us (noise) | 1,482 → 1,372 us (noise) | 1,172 → 1,182 us (noise) |
Peak RSS was the same on both builds: 203-210 MB on macOS, 234-240 MB on Linux with pandas 3.1, and 179-185 MB on Linux with pandas 2.3.
At 10,000 rows the model dominates and the difference is within run-to-run noise. The gain is a fixed ~50-110 us per call, so it matters for online prediction on small DataFrames.
Risk and revert
Low. On pandas 2.3 and 3.1 the feature names were identical for 13 column index types, including categorical and object labels with
None. On pandas 1.x, from the 1.5 source (not run), aNonelabel in aCategoricalIndexmaps toNaN, which the feature-name check incore.pyrejects. With this PR it becomes"nan", as an object Index already gives"None". No input that worked before changes.The cuDF path has the same two patterns. I left it out because I cannot test it without a GPU.
Revert the change in
python-package/xgboost/data.py.