Search code examples
pythondatasetmeanpython-xarray

How do I get the mean value of each hour of a xarray dataset?


I have a datatset with secondly values. The dataconsitency is not so good so sometimes there are also days missing. I want to get the mean value of every hour in this dataset. The time should also contain the date. So that I get something like this:

2015-01-01T00:00:00', '2015-01-01T01:00:00', '2015-05-01T02:00:00'....... and the mean variables to it.

I tried:

ds_mean = ds.groupby("time.hour").mean()

But there it gives me a mean value of all hours a this time containing the dataset.

Then I tried to create a dataset for each day. But the problem is that the time only contains the hour.


Solution

  • Instead of groupby, use ds.resample. Groupby groups on the unique hour values (1-24) which appear in the data, whereas resample explicitly changes the frequency of a DateTimeIndex dimension:

    ds_mean = ds.resample(time="H").mean()
    

    See the user guide section on working with time series data for more info and examples.