← Back to Pull requests

[python] Iterate pandas columns with items() instead of a name lookup per column #12815

Closed
unknown closed this pull request without merging on Nov 09, 2026 10:00 UTC
View on GitHub
unknown unknown commented on Nov 06, 2026 18:05 UTC
Author

What

Two changes to the pandas input path in python-package/xgboost/data.py:

  • pandas_feature_info builds the feature names with [str(col) for col in data.columns] instead of list(data.columns.map(str)).

  • pandas_transform_data loops over data.items() instead of looking up every column by name with data[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_proba on 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_df takes 489 us of a 594 us predict_proba call. 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, while data.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.py gives 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 timed predict_proba on the first 1, 100 and 10,000 rows, 1000 calls each (50 for 10,000 rows), and took the median. I ran 3 rounds alternating master and 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_proba time 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), a None label in a CategoricalIndex maps to NaN, which the feature-name check in core.py rejects. 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.

    Diff: 433 lines (+324 / -109) across 5 files Reviewer: RAMitchell Review duration: 3835m
    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
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.2% (MEDIUM) · Higher = larger blast radius
    45.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 17.8% · Safe to merge
    17.8%
    Repository