[Python] Pandas, resampling with weighted average

Discussão em 'Python' iniciado por Stack, Março 28, 2025.

  1. Stack

    Stack Membro Participativo

    I want to know how to perform a resampling with a weighted average on each columns based on the time between each measurement.

    Here is an extract of the dataframe (the first column is in m³/h and the second in %):

    DEBITS VOLETS
    datetime
    2014-01-21 00:03:03 NaN 49.93
    2014-01-21 00:09:54 55.40 NaN
    2014-01-21 00:12:59 NaN 47.72
    2014-01-21 00:19:51 48.18 NaN
    2014-01-21 00:22:57 NaN 49.44
    2014-01-21 00:30:00 65.50 NaN
    2014-01-21 00:33:04 NaN 49.37
    2014-01-21 00:39:55 63.24 NaN
    2014-01-21 00:43:00 NaN 49.69
    2014-01-21 00:49:52 65.13 NaN
    2014-01-21 00:52:57 NaN 48.75
    2014-01-21 00:59:59 47.75 NaN
    2014-01-21 01:03:05 NaN 48.50
    2014-01-21 01:09:57 61.09 NaN
    2014-01-21 01:13:01 NaN 48.16
    2014-01-21 01:19:51 58.56 NaN
    2014-01-21 01:22:57 NaN 50.09
    2014-01-21 01:29:59 62.69 NaN
    2014-01-21 01:33:04 NaN 48.55
    2014-01-21 01:39:56 56.73 NaN
    2014-01-21 01:43:01 NaN 49.06
    2014-01-21 01:49:52 56.73 NaN
    2014-01-21 01:52:57 NaN 48.73
    2014-01-21 01:59:58 62.60 NaN


    I know I need to use a function as a parameter of "how" but I do not know how to articulate it.

    df.resample('H', how='mean')


    But I think the np.average of the numpy library function should be used, but NaN seems to cause an error.

    np.average(data['DEBITS'], weights=data.index)


    How to articulate this two function together to make this weighted average?

    Continue reading...

Compartilhe esta Página