[Python] Deprecation Warning with groupby.apply

Discussão em 'Python' iniciado por Stack, Julho 3, 2025.

  1. Stack

    Stack Membro Participativo

    I have a python script that reads in data from a csv file

    The code runs fine, but everytime it runs I get this Deprecation message:

    DeprecationWarning: DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated, and in a future version of pandas the grouping columns will be excluded from the operation. Either pass `include_groups=False` to exclude the groupings or explicitly select the grouping columns after groupby to silence this warning.


    the warning stems from this piece of code:

    fprice = df.groupby(['StartDate', 'Commodity', 'DealType']).apply(lambda group: -(group['MTMValue'].sum() - (group['FixedPriceStrike'] * group['Quantity']).sum()) / group['Quantity'].sum()).reset_index(name='FloatPrice')


    to my understanding, I am performing the apply function on my groupings,but then I am disregarding the groupings and not using them anymore to be apart of my dataframe. I am confused about the directions to silence the warning

    here is some sample data that this code uses:

    TradeID TradeDate Commodity StartDate ExpiryDate FixedPrice Quantity MTMValue
    -------- ---------- --------- --------- ---------- ---------- -------- ---------
    aaa 01/01/2024 (com1,com2) 01/01/2024 01/01/2024 10 10 100.00
    bbb 01/01/2024 (com1,com2) 01/01/2024 01/01/2024 10 10 100.00
    ccc 01/01/2024 (com1,com2) 01/01/2024 01/01/2024 10 10 100.00


    and here is the expected output from this data:

    TradeID TradeDate Commodity StartDate ExpiryDate FixedPrice Quantity MTMValue FloatPrice
    -------- ---------- --------- --------- ---------- ---------- -------- --------- ----------
    aaa 01/01/2024 (com1,com2) 01/01/2024 01/01/2024 10 10 100.00 0
    bbb 01/01/2024 (com1,com2) 01/01/2024 01/01/2024 10 10 100.00 0
    ccc 01/01/2024 (com1,com2) 01/01/2024 01/01/2024 10 10 100.00 0

    Continue reading...

Compartilhe esta Página