Skip to content

merge_asof(left_index=True, right_on=...) overwrites left index with right index #33463

Closed
@gtg944q

Description

@gtg944q

Building from an example in the docs...

left = pd.DataFrame({'left_val': ['a', 'b', 'c'], 'left_time': [1, 5, 10]}, index=[1, 5, 10])
right = pd.DataFrame({'right_val': [1, 2, 3, 6, 7]}, index=[1, 2, 3, 6, 7])
>>> pd.merge_asof(left, right, left_index=True, right_index=True)
   left_val  left_time  right_val
1         a          1          1
5         b          5          3
10        c         10          7

Returned index is the index of left.

>>> pd.merge_asof(left, right, left_on='left_time', right_index=True)
   left_val  left_time  right_val
1         a          1          1
5         b          5          3
10        c         10          7

Returned index is the index of left.

>>> pd.merge_asof(left, right, left_on='left_time', right_on='right_val')
  left_val  left_time  right_val
0        a          1          1
1        b          5          3
2        c         10          7

Return value has index reset (I assume expected?)

>>> pd.merge_asof(left, right, left_index=True, right_on='right_val')
  left_val  left_time  right_val
1        a          1          1
3        b          5          3
7        c         10          7

Returned index is the index of right.

Perhaps I'm misunderstanding something about how this is supposed to work, but it seems to me that the behavior in the last example is incorrect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions