-
In xarray seasonal grouping. I need to group by following group of months: JF,MAM,JJAS,OND and sum the grouped season values to perform trend analysis, whereas default groupby.season months are DJF,MAM,JJA,SON. Code: import xarray as xr
import numpy as np
import pymannkendall as mk
Data = xr.open_dataset(in_file)
print(Data)
Data = Data['rain'].groupby('time.season').sum('time')
Data = Data.sel(lon= np.arange(75,81,0.25), lat = np.arange(8, 14,0.25),method ='nearest')
output=[]
for i in np.arange(len(Data.lat.values)):
for j in np.arange(len(Data.lon.values)):
try:
slope_val = mk.original_test(Data[:,i,j]).z
except:
slope_val=-9999
output.append(slope_val)
output = np.copy(output).reshape(Data.lat.size,Data.lon.size)
score = xr.DataArray(output, dims=('lat','lon'), coords={'lat':Data.lat, 'lon':Data.lon}, name='ZScore')
df = score.to_dataframe() |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
You can pass a DataArray with your self-defined groups to GroupBy . See #6180 on one way how to define the groups (no need to apply the |
Beta Was this translation helpful? Give feedback.
-
On latest Xarray (>2024.04.0) this works: import xarray as xr
from xarray.groupers import SeasonGrouper
ds = xr.tutorial.open_dataset("air_temperature")
ds.groupby(time=SeasonGrouper(["JF", "MAM", "JJAS", "OND"])).mean() https://docs.xarray.dev/en/latest/user-guide/time-series.html#handling-seasons |
Beta Was this translation helpful? Give feedback.
On latest Xarray (>2024.04.0) this works:
https://docs.xarray.dev/en/latest/user-guide/time-series.html#handling-seasons