Skip to content

ENH: allow pd.merge() variables right_on and left_on to accept pd.Index() array_likes #58279

Open
@loewenm

Description

@loewenm

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

When passing a Pandas Index as a variable in either right_on or left_on in a pd.merge(), the result is a KeyError

KeyError: array(['label 1', 'label 2', 'label 3'], dtype=object)

The documentation says that you can pass "label or list, or array-like". While pd.Index() might not be exactly an array-like, it does seem counterproductive to exclude the very thing that should be referenced when merging...

Feature Description

Simple request: Please expand allowable datastructures for right_on and left_on to be "label, list, array-like, or index".

Alternative Solutions

Temporary solution is to covert pd.Index to_lists():

merged_df = pd.merge(
left=df_A,
right=df_B,
right_on=df_A.columns.to_list(),
left_on=df_B.columns.to_list(),
how="left",
)

Additional Context

I believe this probably just a simple "oh-yeah" moment...

Metadata

Metadata

Labels

EnhancementNeeds TriageIssue that has not been reviewed by a pandas team memberReshapingConcat, Merge/Join, Stack/Unstack, Explode

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions