[Python] How do I create hourly means with Pandas only when I have at least half of the data...

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

  1. Stack

    Stack Membro Participativo

    I have a Pandas dataframe df with a datetime index and three columns, like this:

    Out[64]:
    rh pm25a pm25b
    time_stamp
    2022-07-06 11:35:53-04:00 49.0 13.18 14.13
    2022-07-06 11:37:53-04:00 47.0 13.33 14.77
    2022-07-06 11:39:53-04:00 45.0 12.41 14.79
    2022-07-06 11:41:54-04:00 44.0 12.30 15.28
    2022-07-06 11:43:54-04:00 43.0 13.85 14.46
    ... ... ...
    2025-05-31 19:50:17-04:00 36.0 7.00 6.00
    2025-05-31 19:52:17-04:00 37.0 6.70 7.30
    2025-05-31 19:54:17-04:00 37.0 6.70 6.40
    2025-05-31 19:56:17-04:00 37.0 7.20 6.70
    2025-05-31 19:58:17-04:00 38.0 7.10 6.90

    [640031 rows x 3 columns]


    The data points come every two minutes, so I have 30 data point per hour. However, the instrument happens to fail now and then and I might have fewer. For the hours with less than 20 data points I would like to get a NaN as result.

    I am doing hourly means with:

    df_H = df.resample('h').mean()


    which works fine, but I cannot figure out how to set a condition on the number of data points within each hour.

    Continue reading...

Compartilhe esta Página