Feature Type
-
[ ] Adding new functionality to pandas
-
[X] Changing existing functionality in pandas
-
[ ] Removing existing functionality in pandas
Problem Description
For a data series with datetime index... I wish there is an easy way to make the very first element to be on the left side of the window when rolling starts.
As of pandas 2.1.4 I can align a rolling window so the very first element of my series is on the right side of the window when the rolling starts (i.e. center=False). I can also align the verify first element to be in the middle of the window when rolling starts (i.e. center=True)
But there is no easy way to make the very first element to be on the left side of the window when rolling starts.
For integer window I can see that I can use FixedForwardWindowIndexer as shown in the following page https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.api.indexers.FixedForwardWindowIndexer.html#pandas.api.indexers.FixedForwardWindowIndexer
But that doesn't seem to work for when the index is datetime.
Feature Description
import pandas as pd
# create series
s = pd.Series(range(5), index=pd.date_range('2020-01-01', periods=5, freq='1D'))
# create a function to call for each window
def roll_apply(window):
# return first element of current window
return window[:1].iloc[0]
# if center=False, the very first window will have the first element of my series on its right most side
right = s.rolling('4D', center=False).apply(roll_apply)
# if center=False, the very first window will have the first element of my series on its center
center = s.rolling('4D', center=True).apply(roll_apply)
# there is no easy way to have the very first window have the first element of my series on its left
# ???
print("********* original *********")
print(s)
print("********* right aligned *********")
print(right)
print("********* center aligned *********")
print(center)
print("********* left aligned *********")
print("Not Implemented Yet!")
output
********* original *********
2020-01-01 0
2020-01-02 1
2020-01-03 2
2020-01-04 3
2020-01-05 4
Freq: D, dtype: int64
********* right aligned *********
2020-01-01 0.0
2020-01-02 0.0
2020-01-03 0.0
2020-01-04 0.0
2020-01-05 1.0
Freq: D, dtype: float64
********* center aligned *********
2020-01-01 0.0
2020-01-02 0.0
2020-01-03 1.0
2020-01-04 2.0
2020-01-05 3.0
Freq: D, dtype: float64
********* left aligned *********
Not Implemented Yet!
Alternative Solutions
For integer window I can see that I can use FixedForwardWindowIndexer as shown in the following page https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.api.indexers.FixedForwardWindowIndexer.html#pandas.api.indexers.FixedForwardWindowIndexer
But that doesn't seem to work for when the index is datetime.
Additional Context
No response