Applying custom functions to rolling #6247
-
Hello, What is the correct way of implementing a custom function to the rolling method in xarray. For instance, with a pandas DataFrame if I wanted to do something like a cumulative sum with a decay constant over a 7-day window, this operation is quite trivial: def decayed_sum(x, k=0.5):
sum_ = 0
for i in range(1, x.size+1):
sum_ += x[-1]*k**i
return sum_
df = pd.DataFrame({'value':np.random.negative_binomial(5, 0.9, size=60)})
df.rolling(window=7).apply(decayed_sum) However, with an xarray DataArray I'm at a loss as to how this works or translates to the same operation that would be applied over space and time. So how would the da = xr.DataArray(
np.random.negative_binomial(5, 0.9, size=(60, 10, 10)),
coords=[range(60), range(10), range(10)],
dims=["time", "x", "y"],
) Looking for examples has proved difficult, just wondering if anyone can help me out here? Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 6 replies
-
HI @rhyswhitley , sorry you found this tricky. If you have any ideas as to how our documentation could be clearer we are all ears! That said, do you not just need
Can you explain a bit more about what you want to do here? You want to use a 1D function to reduce over a 2D rolling window? |
Beta Was this translation helpful? Give feedback.
-
Hello dear Xarray team. I have the same problem, which I've been failing to solve. Lest to deviate attention from the original question, I will, simply, ask clarifying questions about the provided solutions if you don't mind. The first question is about the function The second question is about the function And let me formulate my wish to the team please. But don't take it as a sign of impudence; we, users, tend to have wishes. You, maintainers, can tell me to get lost with my wish. That's perfectly fine. Thank you in advance. |
Beta Was this translation helpful? Give feedback.
-
@TomNicholas thank you for taking time to explain all that to me! With regards to the With regards to my second question, I still don't have a clear understanding of it. But you pointed out the directions I should look at, which is helpful indeed, thank you. You mentioned that To answer your last questions I will have to use my case as an example. I think your very last sentence downplays the complexity associated with the trick. Here's why. My understanding of the statement "It sounds like Once one wants to actually reduce the dimensions of the rolling window, one has to bother with the From that perspective, my answer to your question what other kinds of functions I have in mind is all the functions that imply reduction of the rolling window dimensions because all of them will require additional work from the user associated with You say that kind of work is unavoidable. I say ok, but there are cases when that kind of work becomes increasingly complicated and my wish is to make it less complicated. Here's an example of a more complicated case (my case). I have a 2D vector field (representing a 2D slice of a water flow). Each vector is a velocity. I store my field in an I understand I can find a solution to my case. But I've been looking for it for 2 days already to no avail. I think I would spend at least a week on it didn't I give up and decide to try |
Beta Was this translation helpful? Give feedback.
-
You can iterate over the rolling object. Does that help? We could add |
Beta Was this translation helpful? Give feedback.
-
Hello @dcherian, I think I could iterate, of course, if I had all the time for me, till the Sun explodes.
It means that - for more general cases - either Searching the Internet, I have discovered another solution which worked in my case. The solution consists of several bits and, man, was it a pain in the rear side to piece those bits together! As you see, this solution takes 6 steps. And one has to figure out those steps. From this perspective, your There is one issue I would like to bring up though. I had solved my problem with mere And here we are: I think that if you just copy pandas syntax, you won't make Thank you in advance. |
Beta Was this translation helpful? Give feedback.
-
This is a straightforward example of parallel computing a function. I hope it can assist those who encounter similar situations.
|
Beta Was this translation helpful? Give feedback.
HI @rhyswhitley , sorry you found this tricky. If you have any ideas as to how our documentation could be clearer we are all ears!
That said, do you not just need
DataArrayRolling.reduce(func)
? I.e.da.rolling(window=7).reduce(decayed_sum, k=0.5)
Can you explain a bit more about what you want to do here? You want to use a 1D function to reduce over a 2D rolling window?