Skip to content

ENH: say which DataFrame is missing the key in merge KeyError (GH#71819) - #71954

Draft
Eagle-Scout wants to merge 1 commit into
pandas-dev:mainfrom
Eagle-Scout:enh-merge-keyerror-side-71819
Draft

Eagle-Scout wants to merge 1 commit into
pandas-dev:mainfrom
Eagle-Scout:enh-merge-keyerror-side-71819

Conversation

@Eagle-Scout

Copy link
Copy Markdown

When a key passed to on, left_on or right_on was missing, merge/join raised a bare KeyError: 'key' that did not say which DataFrame lacked it. The error now names the side and lists the available columns and index levels, e.g.

KeyError: "'customer_id' not found in right DataFrame. Available
columns: ['id', 'name']"

If an on key is missing from both DataFrames, both are reported. The exception type is still KeyError and the original error is chained as __cause__.

  • Add _MergeOperation._get_merge_key_values and use it for all key lookups in _get_merge_keys
  • Update the join(on=...) test in test_join.py for the new message
  • Add TestMergeMissingKeyError to test_merge.py
  • Add a whatsnew entry to v3.2.0.rst

AI use disclosure: I used Claude Code, model Opus 5.5 on low effort. It was used to explain in detail what the issue entailed, the location within the repository, including the affected files that called on or tested merge.py. It was used to help generate a some rudimentary functions and testing, along with some of the entry in the latest whatsnew snapshot. TLDR is I used a little bit of Claude Opus 5.5 in my workflow.

closes #71819

When a key passed to ``on``, ``left_on`` or ``right_on`` was missing,
merge/join raised a bare ``KeyError: 'key'`` that did not say which
DataFrame lacked it. The error now names the side and lists the
available columns and index levels, e.g.

    KeyError: "'customer_id' not found in right DataFrame. Available
    columns: ['id', 'name']"

If an ``on`` key is missing from both DataFrames, both are reported.
The exception type is still ``KeyError`` and the original error is
chained as ``__cause__``.

- Add _MergeOperation._get_merge_key_values and use it for all key
  lookups in _get_merge_keys
- Update the join(on=...) test in test_join.py for the new message
- Add TestMergeMissingKeyError to test_merge.py
- Add a whatsnew entry to v3.2.0.rst

closes pandas-dev#71819

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ENH: DataFrame.merge raises bare KeyError without saying which DataFrame is missing the key column

1 participant