'Python Xarray integrate a 2D array along datetime dimension

I have very large 2D variables in Xarray. They have the form: counts(time, altitude) where time is a numpy datetime every 10 seconds, altitude is float, counts are floats with occasional NaNs.

I would like to reduce the resolution to every 15 minutes by summing or averaging over the corresponding columns.

Likewise, I would like to do the same along the rows of counts in the altitude dimension.

I would appreciate some advice on how this should be done in Python (I'm still on the learning curve for Python).



Solution 1:[1]

You could use the resample method from xarray (see examples in docs https://xarray.pydata.org/en/stable/generated/xarray.Dataset.resample.html).

There are named time offsets based on pandas (see https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#dateoffset-objects).

For example, a yearly mean based on a daily database should be something like this:

ds.resample(time='Y').mean('time')

In your case it should be:

ds.resample(time='15min').mean('time')

where time is the time variable in your dataset and '15min' is the named time offset from https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#dateoffset-objects

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman