Average over a specific time period

前端 未结 2 2035
清酒与你
清酒与你 2021-01-21 18:54

I have a quite huge table in python from a .h5 file The start of the table looks somewhat like this:

table =
                [WIND REL DIRECTION  [deg]]  [WIND S         


        
2条回答
  •  灰色年华
    2021-01-21 19:44

    I think this is the "right" way to do this. (Although it seems a little bit underdocumented to me. Anyway it works!)

    You need to do a groupby on your DataFrame and use something called a TimeGrouper.

    It works like this:

    import pandas as pd
    import numpy as np
    
    # Create a dataframe. You can ignore all this bit!
    periods = 60 * 60
    random_dates = pd.date_range('2015-12-25', periods=periods, freq='s')
    random_speeds = np.random.randint(100, size=periods)
    random_directions = np.random.random(periods)
    df = pd.DataFrame({'date': random_dates, 'wind_speed': random_speeds, 'wind_direction': random_directions})
    df = df.set_index('date')
    
    # Here's where the magic happens:
    grouped15s = df.groupby(pd.TimeGrouper(freq='15S'))
    averages_ws_15s = grouped15s.wind_speed.mean()
    

    Or, if you insist on having spaces in your column names, that last line will become:

    averages_ws_15s = grouped15s['Wind Speed'].mean()
    

    This results in the following:

    date
    2015-12-25 00:00:00    45.800000
    2015-12-25 00:00:15    48.466667
    2015-12-25 00:00:30    38.066667
    2015-12-25 00:00:45    54.866667
    2015-12-25 00:01:00    34.866667
    2015-12-25 00:01:15    37.000000
    2015-12-25 00:01:30    47.133333
    etc....                etc....
    

提交回复
热议问题