-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
rolling: allow control over padding #2007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Very useful suggestion.
We already support a different type of "rolling" with periodicity I suspect this would not be too hard to implement. |
What do you mean by Specifically, what's the |
Probably a mix of both - I want to compute a moving average, but with periodic boundaries.
and so on... |
I think what I want is like the I found two possibilities but they are quite "hand made" and certainly not very efficient Solution with slicing: # take the last and first elements and append/ prepend them
first = ds[:15]
last = ds[-15:]
extended = xr.concat([last, ds, first], 'dayofyear')
# do the rolling on the extended ds and get rid of NaNs
sol1 = extended.rolling(dayofyear=31, center=True).mean().dropna('dayofyear') Solution with roll1 = ds.roll(dayofyear=150).rolling(dayofyear=31, center=True).mean()
roll2 = ds.rolling(dayofyear=31, center=True).mean()
sol2 = xr.concat([roll1, roll2], dim='r').mean('r') Call |
I think the implementation would be not so difficult by supporting more flexible Maybe da.rolling(dayofyear=31).construct('window', fill_value='periodic').mean('window') @mathause, any interest in contributing? |
Though I'm not sure you need the IIUC you need to copy a window-sized amount of data from the front of the array onto the back. You could do that with construct-like machinery, which would save a copy - though not a large copy |
@maxim-lian , do you agree to add this feature? |
@fujiisoup Yes for sure - I think it would be good. I think there are two salient questions:
|
Agreed.
Only a slight modification of |
#2011 looks good - I didn't realize numpy already had Agree with your other comments. Thanks as ever @fujiisoup |
I was going to suggest this feature so glad others are interested. In my use case I would like to smooth a daily climatology. My colleague uses matlab and uses https://www.mathworks.com/matlabcentral/fileexchange/52688-nan-tolerant-fast-smooth Using the
|
I am coming back to @shoyer suggestion in #2011 - your idea would be to do first a import numpy as np
import xarray as xr
x = np.arange(1, 366)
y = np.random.randn(365)
ds = xr.DataArray(y, dims=dict(dayofyear=x))
ds.pad(dayofyear=15, mode='wrap').rolling(center=True, dayofyear=31).mean() |
I think you may need to do cropping afterwards, too, before taking the mean. |
I just need to find the three warmest consecutive months from a temperature dataset for my work, so I thought I add a complete example. First, create an example dataset with monthly temperature: import xarray as xr
import numpy as np
import pandas as pd
time = pd.date_range("2000", periods=12 * 30, freq="M")
temp = np.sin((time.month - 5) / 6 * np.pi) + np.random.randn(*time.shape) * 0.3
da = xr.DataArray(temp, dims=["time"], coords=dict(time=time))
print(da) <xarray.DataArray (time: 360)>
array([-0.676731, -0.812742, -1.367547, ..., 0.186731, 0.237676, -0.343879])
Coordinates:
* time (time) datetime64[ns] 2000-01-31 2000-02-29 ... 2029-12-31 Currently we can achieve this like: n_months = 3
monthly = da.groupby("time.month").mean()
padded = monthly.pad(month=n_months, mode="wrap")
rolled = padded.rolling(center=True, month=n_months).mean(skipna=False)
sliced = rolled.isel(month=slice(3, -3))
central_month = sliced.idxmax() Implementing monthly = da.groupby("time.month").mean()
rolled = monthly.rolling(center=True, month=n_months, pad_mode="wrap").mean(skipna=False)
central_month = rolled.idxmax() |
I think we should do |
Hello! First of all, thanks so much for those of you who contribute to xarray, I've found it super useful as an n-dimensional extension of pandas! I was just wondering what the current state of this issue is? I'm running into exactly the issue described in #4743 which seems like a bug; that issue was closed as a dupe of this. Are we just waiting for someone to implement something here, or are there other blockers? |
This should be easy now so we just need someone to try it out. This is where the padding happens so the kwarg needs to be passed all the way down to xarray/xarray/core/variable.py Line 2132 in 67d1955
What should the API be? |
For this API, it seems that the only thing that would need to be implemented would be adding a |
Yes. I think we might want this anyway; for |
Code Sample, a copy-pastable example if possible
Problem description
rolling
cannot directly handle periodic boundary conditions (lon, dayofyear, ...), but could be very helpful to e.g. calculate climate indices. Also I cannot really think of an easy way to append the first elements to the end of the dataset and then calculate rolling.Is there a way to do this? Should xarray support this feature?
This might also belong to SO...
The text was updated successfully, but these errors were encountered: