I am trying to detrend some daily climate data that extends over 74 years and some spatial extent (longitude and latitude). I would like to detrend the 74 year time series for each day of the year individually so that I can have value of the detrended time series coincide with the non-detrended time series for each day of year and longitude/latitude location in the first year.
Currently I am looping over each longitude, latitude, month of year and day of month of an xarray dataarray X and extracting a single time series (of length 74) using something like:
for im, m in enumerate(months):
for id in range(monthdays[im]):
for j, lon in enumerate(lons):
for k, lat in enumerate(lats):
ts = X.loc[{'time': (X['time.month'] == m) & (X['time.day'] == (id+1)),
'lon': lons[j], 'lat': lats[k]}]
# some code to detrend
detrended = detrend(ts)
ts = detrended # this only modifies the copy and not the original
X.loc[{'time': (X['time.month'] == m) & (X['time.day'] == (id+1)),
'lon': lons[j], 'lat': lats[k]}] = detrended # this doesn't work either
I’d like to modify X directly using this loop but ts is a copy and not a view of the data so this doesn’t work. What is the best way to achieve this?