How to use groupby or resample to downsample hourly data to group data according to day hour index of year in python?

Krishna Kashiv

There are functions that can group data into hourly i.e. 24 or into day of year i.e. 365. I have a dataset of 3 years 1999-2001 that has hourly values. So total values are 24*365*4+1*24=26304(1*24= leap year day). When I run the function

result=ds.groupby('time.dayofyear').mean('time')

The result it gives:

<xarray.DataArray 'precip' (dayofyear: 366, lat: 21, lon: 33)>
array([[[0.        , 0.        , 0.        , ..., 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , ..., 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , ..., 0.        ,
         0.        , 0.        ],
        ...,
        [0.00086806, 0.00065104, 0.00097656, ..., 0.        ,
         0.        , 0.        ],
        [0.00141059, 0.00141059, 0.00130208, ..., 0.        ,
         0.        , 0.        ],
        [0.00195312, 0.00141059, 0.00119358, ..., 0.        ,
         0.        , 0.        ]],

       [[0.        , 0.        , 0.        , ..., 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , ..., 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , ..., 0.        ,
         0.        , 0.        ],
        ...,]

Coordinates:
  * lon        (lon) float32 220.0 222.5 225.0 227.5 ... 292.5 295.0 297.5 300.0
  * lat        (lat) float32 20.0 22.0 24.0 26.0 28.0 ... 54.0 56.0 58.0 60.0
  * dayofyear  (dayofyear) int64 1 2 3 4 5 6 7 8 ... 360 361 362 363 364 365 366

If I use the time.hour groupby function:

result=ds.groupby('time.hour').mean('time')
<xarray.DataArray 'precip' (hour: 24, lat: 21, lon: 33)>
array([[[0.        , 0.        , 0.        , ..., 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , ..., 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , ..., 0.        ,
         0.        , 0.        ],
        ...,
        [0.00015682, 0.00022097, 0.00047759, ..., 0.        ,
         0.        , 0.        ],
        [0.00033503, 0.00037779, 0.0004562 , ..., 0.        ,
         0.        , 0.        ],
        [0.00044195, 0.00039918, 0.00039205, ..., 0.        ,
         0.        , 0.        ]],, dtype=float32)
Coordinates:
  * lon      (lon) float32 220.0 222.5 225.0 227.5 ... 292.5 295.0 297.5 300.0
  * lat      (lat) float32 20.0 22.0 24.0 26.0 28.0 ... 52.0 54.0 56.0 58.0 60.0
  * hour     (hour) int64 0 1 2 3 4 5 6 7 8 9 ... 14 15 16 17 18 19 20 21 22 23

How to groupy hour of the year where it gives me hourly average of the year rather than a day. Need the function to give result as 366*24 =8784 where average is calculated using day hour index.

spencerkclark

I think you are asking for the same thing as in a question I answered earlier. In short, I think the cleanest approach in xarray at the moment is to use strftime to generate a coordinate with the "hourofyear" values for each date and use groupby on that:

ds['hourofyear'] = xr.DataArray(ds.time.dt.strftime('%m-%d %H'), coords=ds.time.coords)
result = ds.groupby('hourofyear').mean('time')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use pandas resample using 'day of year' data (Python)

From Dev

xarray - Use groupby to group by every day over a year's climatological hourly netCDF data

From Dev

How to select one hour each day from hourly data in R?

From Dev

How to groupby and resample data in pandas?

From Dev

Group Pandas data by hour of the day

From Python

How to resample daily data to hourly data for all whole days with pandas?

From Dev

How to resample time series dataframe to show average hourly data?

From Dev

how can i group data per day and per hour on Mongo?

From Dev

How to group data by hour of the day in mysql when there are multiple timestamps?

From Dev

How to calculate total precipitation per day using hourly data for whole year?

From Dev

Python resample by day & get weekstart data

From Dev

How to group data by every hour

From Dev

R: how to resample intraday data at the group level?

From Dev

How to resample the data to 'Odd(instead of even)' 2 hour timeframe?

From Dev

Pandas group hourly data into daily sums with date index

From Dev

Python Dataframe-How to groupby three different columns consisting Year, Month, Day data and calculate sum from fourth column

From Dev

Dataframe Resample with GroupBy on time data

From Dev

How to use pandas to resample time series data

From Dev

From half-hour to hourly data

From Dev

How to get data at the end of the hour of the day

From Dev

What is the fastest way to repeatedly resample timeseries data of the same shape from hourly to yearly in python

From Dev

How to group data by the month and year

From Dev

Resample data to add missing hour values

From Dev

How do I sum time series data by day in Python? resample.sum() has no effect

From Dev

How to extract hourly data from a df in python?

From Dev

How to average hourly data over a 2 day period

From Dev

Python: How to convert a range of hours to Month, Day, Hour of a year

From Dev

How to groupby pandas datetime with hourly and day?

From Dev

How to aggregate data hourly?

Related Related

  1. 1

    How to use pandas resample using 'day of year' data (Python)

  2. 2

    xarray - Use groupby to group by every day over a year's climatological hourly netCDF data

  3. 3

    How to select one hour each day from hourly data in R?

  4. 4

    How to groupby and resample data in pandas?

  5. 5

    Group Pandas data by hour of the day

  6. 6

    How to resample daily data to hourly data for all whole days with pandas?

  7. 7

    How to resample time series dataframe to show average hourly data?

  8. 8

    how can i group data per day and per hour on Mongo?

  9. 9

    How to group data by hour of the day in mysql when there are multiple timestamps?

  10. 10

    How to calculate total precipitation per day using hourly data for whole year?

  11. 11

    Python resample by day & get weekstart data

  12. 12

    How to group data by every hour

  13. 13

    R: how to resample intraday data at the group level?

  14. 14

    How to resample the data to 'Odd(instead of even)' 2 hour timeframe?

  15. 15

    Pandas group hourly data into daily sums with date index

  16. 16

    Python Dataframe-How to groupby three different columns consisting Year, Month, Day data and calculate sum from fourth column

  17. 17

    Dataframe Resample with GroupBy on time data

  18. 18

    How to use pandas to resample time series data

  19. 19

    From half-hour to hourly data

  20. 20

    How to get data at the end of the hour of the day

  21. 21

    What is the fastest way to repeatedly resample timeseries data of the same shape from hourly to yearly in python

  22. 22

    How to group data by the month and year

  23. 23

    Resample data to add missing hour values

  24. 24

    How do I sum time series data by day in Python? resample.sum() has no effect

  25. 25

    How to extract hourly data from a df in python?

  26. 26

    How to average hourly data over a 2 day period

  27. 27

    Python: How to convert a range of hours to Month, Day, Hour of a year

  28. 28

    How to groupby pandas datetime with hourly and day?

  29. 29

    How to aggregate data hourly?

HotTag

Archive