RollingWindow on DataFrame
See original GitHub issueHello again,
If you have a minute, would you mind helping me figure out why I can’t get rollingWindow
to do what I expect?
I have a bunch of data formatted like so:
__index__ BTC ETH
---------- ------- -------
2017-06-15 2261.25 333.499
2017-06-16 2481.74 371.358
2017-06-17 2591.41 373.975
2017-06-18 2568.92 377.562
2017-06-19 2578.25 371.813
2017-06-20 2657.32 376.359
2017-06-21 2709.33 338.292
2017-06-22 2714.58 340.326
2017-06-23 2760.61 340.811
2017-06-24 2688.44 334.167
2017-06-25 2627.95 321.283
2017-06-26 2476.72 262.385
2017-06-27 null null
2017-06-28 2539.78 301.789
2017-06-29 2536.94 315.543
2017-06-30 2547.95 305.518
2017-07-01 2507.89 282.877
2017-07-02 2445.84 269.758
2017-07-03 2519.06 288.076
2017-07-04 2618.16 284.342
And I’m running the following:
const performance = df.rollingWindow(2).select(window => {
return ((window.last() - window.first()) / window.first())
})
.withIndex(pair => pair[0])
.select(pair => pair[1])
console.log(performance.toString());
But I keep getting…
__index__ __value__
--------- ---------
0 NaN
1 NaN
2 NaN
3 NaN
4 NaN
5 NaN
6 NaN
7 NaN
8 NaN
9 NaN
10 NaN
// truncated for brevity
353 NaN
354 NaN
355 NaN
356 NaN
357 NaN
358 NaN
359 NaN
360 NaN
361 NaN
362 NaN
363 NaN
Not sure what I’m doing wrong. I’ve tried several variations based on the examples on the Guide.
Thank you!
Issue Analytics
- State:
- Created 5 years ago
- Comments:16 (10 by maintainers)
Top Results From Across the Web
pandas.DataFrame.rolling — pandas 1.5.2 documentation
Provide rolling window calculations. ... Size of the moving window. If an integer, the fixed number of observations used for each window. If...
Read more >Python | Pandas dataframe.rolling()
Pandas dataframe.rolling() function provides the feature of rolling window calculations. The concept of rolling window calculation is most ...
Read more >How rolling() Function works in Pandas Dataframe?
Pandas rolling () function gives the element of moving window counts. The idea of moving window figuring is most essentially utilized in signal...
Read more >Don't Miss Out on Rolling Window Functions in Pandas
1. Window Rolling Mean (Moving Average) ... The moving average calculation creates an updated average value for each row based on the window...
Read more >Pandas Series: rolling() function
Rolling window calculations in Pandas ... The rolling() function is used to provide rolling window calculations. ... Example - Contrasting to an ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes I see the problem (although I haven’t tested the code myself).
You are doing this:
Instead you probably want this:
The reason you return the pair (index + value) is because you want to reintegrate the new series with the original dataframe, like this:
Merging the new series into the original dataframe gets you the combined results.
If you don’t want to merge you can simplify your code to this:
Note that you can simplify further by using the more concise anonymous function syntax as follows:
Also consider using either of the following convenience functions that are built in 😃
For example:
Or
You could do something like this:
Haven’t tested this code! Let me know if it helps!