1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Parse dataframe into list of subinterval dataframes for processing

Discussão em 'Python' iniciado por Stack, Setembro 27, 2024 às 19:33.

  1. Stack

    Stack Membro Participativo

    I have a dataframe, esu_tos:

    Time TickType Price Size
    2023-08-13 15:10:46.166 1 4487.25 1
    2023-08-13 15:10:47.375 1 4487 1
    2023-08-13 15:10:54.656 1 4487.25 2
    2023-08-13 15:10:57.627 1 4487 1
    2023-08-13 15:10:57.628 1 4487 1
    2023-08-13 15:10:57.628 1 4487 1
    2023-08-13 15:11:00.759 1 4487.25 1
    2023-08-13 15:11:00.759 1 4487 3
    2023-08-13 15:11:01.415 1 4487 3
    2023-08-13 15:11:01.416 1 4487 1


    That I'd like to parse into sub-intervals like below. Is there a better way to do this aside from the loop?

    interval = '5s' # 1min / 30s / 10s / 5s
    st = datetime(2023,8,13,15,10)
    end = datetime(2023,8,14,14,1)

    rng = pd.date_range(st, end, freq = interval, inclusive='both')


    d = []
    d_t = []
    for i, k in enumerate(rng):
    try:
    a = esu_tos[(esu_tos.Time >= k) & (esu_tos.Time < rng[i+1])]

    except:
    a = esu_tos[(esu_tos.Time >= k) & (esu_tos.Time <= end)]

    d.append(a)
    d_t.append(k)


    Looking specifically to return a list or other object which will allow me to access the data aggregated to each interval -- not simply as pd.Grouper or groupby object.

    For instance, the included code creates a list of dataframes, which I'm subsequently able to access process individually.

    Continue reading...

Compartilhe esta Página