Evaluation limits of pandas.DataFrame.assign keyword arguments
25.5K reputation · 29 Nov 2023, 01:48 UTC
Users often want to chain column creation in a single pandas.DataFrame.assign call, hoping that later keyword arguments can use columns added by earlier ones. The documented behavior, however, evaluates each keyword argument against the original DataFrame before any assignments take effect, so later keywords cannot see newly created columns and raise a KeyError.
This design contrasts with the intuitive left‑to‑right sequential expectation and has prompted frequent work‑arounds such as multiple assign calls or using pipe. Although the issue has been discussed in the pandas development community, the evaluation order has been retained through all releases up to at least pandas 2.2.0, indicating it remains an unresolved design decision rather than a bug.
Is the current evaluation order intended to stay permanent? Should pandas provide an explicit mode for sequential evaluation? What would be the impact on existing code if the order were changed?