[Python] Pandas time series dataframe take random samples by group (date) ignore missing dates

Discussão em 'Python' iniciado por Stack, Maio 2, 2025.

  1. Stack

    Stack Membro Participativo

    I have a time series dataframe, I would like to take x random samples from column "temperature" from each day. I am able to do this with:

    daily_groups = df.groupby([pd.Grouper(key='time', freq='D')])['temperature'].apply(lambda x: x.sample(10))


    This works if there are at least x samples for each day. If there are not I get the error "a must be greater than 0 unless no samples are taken". What I would like is, lets say I want 10 samples, get 10 if available, if not get as many as possible and if there are none skip this day. I don't want to up sample data.

    Also I don't know if its possible to return the original dataframe with the values filtered for items mention above, instead of returning the groupby series object. Thanks for any help.

    Continue reading...

Compartilhe esta Página