Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC: Incorrect explanation of merge_asof with direction="backward" #53279

Open
1 task done
mosew opened this issue May 17, 2023 · 2 comments
Open
1 task done

DOC: Incorrect explanation of merge_asof with direction="backward" #53279

mosew opened this issue May 17, 2023 · 2 comments
Labels
Docs Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@mosew
Copy link

mosew commented May 17, 2023

Pandas version checks

  • I have checked that the issue still exists on the latest versions of the docs on main here

Location of the documentation

https://pandas.pydata.org/docs/reference/api/pandas.merge_asof.html

Documentation problem

For each row in the left DataFrame:
        A “backward” search selects the last row in the right DataFrame whose ‘on’ key is less than or equal to the left’s key.

This is incorrect, as evidenced here:

left = pd.DataFrame(
    {
        "id": ["a"],
        "date": pd.to_datetime(
            ["20080102"]
        ),
    },
)

right = pd.DataFrame(
    {
        "id": ["a"],
        "date": pd.to_datetime([
            "20080101",
        ])
    },
)

pd.merge_asof(left=left, right=right, by="id", on="date")
  id date
  a 2008-01-02

Suggested fix for documentation

For each row in the left DataFrame:
        A “backward” search selects the last row in the **left** DataFrame whose ‘on’ key is less than or equal to the left’s key.
@mosew mosew added Docs Needs Triage Issue that has not been reviewed by a pandas team member labels May 17, 2023
@mosew
Copy link
Author

mosew commented May 17, 2023

I'll add that this works fine when the left and right on names are different and one uses left_on and right_on; in that case I keep the right_on column from the result.

@j-carson
Copy link

j-carson commented Dec 5, 2023

I'm going to say, as someone who just tracked down a problem in my code due to an unexpected behavior of merge_asof direction='backward', that if the behavior is different depending on whether the column names are the same or different and whether you use on or left_on/right_on, then that's actually a bug, not a documentation issue.

@mroeschke mroeschke added Reshaping Concat, Merge/Join, Stack/Unstack, Explode and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

3 participants