[Python] pandas grouper + time shifting

Discussão em 'Python' iniciado por Stack, Setembro 25, 2024.

  1. Stack

    Stack Membro Participativo

    Here my pandas:

    d= {'dates': ['2020-07-16','2020-07-15','2020-07-14','2020-07-13','2020-07-12','2020-07-11','2020-07-10'],
    'T':[3392,3020,663,2877,366,200,100], 'P': [53,91,0,55,3,22,55]}
    df = pandas.DataFrame(data=d)
    df['dates']=pandas.to_datetime(df['dates'])


    If I pack my pandas each 3 days using Grouper:

    for _, grp in df.groupby(pandas.Grouper(key='dates', freq='3D')):#
    print (grp)
    dates T P
    6 2020-07-10 100 55
    5 2020-07-11 200 22
    4 2020-07-12 366 3
    dates T P
    3 2020-07-13 2877 55
    2 2020-07-14 663 0
    1 2020-07-15 3020 91
    dates T P
    0 2020-07-16 3392 53


    Now I want to pack each 3 days but with one days shift:

    8 2020-07-10 100 55
    7 2020-07-11 200 22
    6 2020-07-12 366 3
    dates T P
    11 2020-07-11 200 22
    10 2020-07-12 366 3
    9 2020-07-13 2877 55
    dates T P
    8 2020-07-12 366 3
    7 2020-07-13 2877 55
    6 2020-07-14 663 0
    dates T P
    5 2020-07-13 2877 55
    4 2020-07-14 663 0
    3 2020-07-15 3020 91
    dates T P
    2 2020-07-14 663 0
    1 2020-07-15 3020 91
    0 2020-07-16 3392 53


    How can I stipulate this to Grouper ?

    Continue reading...

Compartilhe esta Página