[Python] pd.merge_asof and returning *all* values where there is a match [duplicate]

Discussão em 'Python' iniciado por Stack, Agosto 5, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to run a pd.merge_asof where all of the values are returned, rather than one per the left side of the merge.

    For example

    df = pd.DataFrame({"Date": ["2020-01-01", "2020-01-01","2020-01-01"],
    "Symbol": ["A","B","C"],
    "Price": [50, 51, 101]})




    df2 = pd.DataFrame({"Date": ["2020-01-02"]})


    I want to return just all records for 'A','B','C' because they are all the most recent records

    However, when I run

    df_inventory_at_time = pd.merge_asof(left=df2,right=df,right_index=True,left_index=True,direction='backward')


    I get only one record , for symbol 'C', price 101

    Date Date_X Date_Y Symbol Price
    1/2/2020 1/2/2020 1/1/2020 C 101

    What can I do to get all of the records to display?

    Thanks

    Continue reading...

Compartilhe esta Página