Search code examples
pythonmeanpython-xarray

Calculate mean of several xarray variables


I have a xarray data array for example:

<xarray.Dataset>
Dimensions:     (lat: 180, lon: 360, time: 360)
Coordinates:
  * time        (time) datetime64[ns] 1990-01-01 1990-02-01 ... 2020-01-01
  * lat         (lat) float64 -89.5 -88.5 -87.5 -86.5 ... 87.5 88.5 89.5
  * lon         (lon) float64 0 1 2 3 ... 357 358 359
Data variables:
    a    (time, lat, lon) float32 dask.array<shape=(360, 180, 360), chunksize=(360, 180, 360)>
    b    (time, lat, lon) float32 dask.array<shape=(360, 180, 360), chunksize=(360, 180, 360)>
    c    (time, lat, lon) float32 dask.array<shape=(360, 180, 360), chunksize=(360, 180, 360)>
    d    (time, lat, lon) float32 dask.array<shape=(360, 180, 360), chunksize=(360, 180, 360)>

How can I calculate the mean of a, b, c, d into a new variable e?

I know that dataset.mean() works on coordinate dimensions right? Such as over time, lat or long, but how can I calculate the mean of the variables themselves? Couldn't find much online about this.

Thanks very much!


Solution

  • You can turn it into an array, and then calculate the mean across the new dimension:

    mean = ds.to_array(dim='new').mean('new')
    ds.assign(e=mean)