Skip to content

ENH: Add VariableForwardWindowIndexer #34226

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

Closed
yohplala opened this issue May 17, 2020 · 3 comments
Closed

ENH: Add VariableForwardWindowIndexer #34226

yohplala opened this issue May 17, 2020 · 3 comments
Labels
Enhancement Window rolling, ewma, expanding

Comments

@yohplala
Copy link

Is your feature request related to a problem?

I would like to use the rolling() method with looking-forward window using a negative window.
I don't remember which other pandas function is using this approach of negative window for reversing the position of the window with respect to the actual index, but it is for the user very convenient.
I was convinced it was existing for rolling, but it appears it is not.

Describe the solution you'd like

# Definition of a negative offset (this works)
offset = pd.tseries.frequencies.to_offset('-1h')

# Use it in rolling (this does not work)
start = '2020-01-01 08:00'
end = '2020-01-01 12:00'
intervals = pd.period_range(start = start, end=end, freq = '30T')
values = [i for i in range(0, len(intervals))]
ser = pd.Series(values, index = intervals)
test = ser.rolling(window = offset, closed='left').min()
@yohplala yohplala added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels May 17, 2020
@mroeschke
Copy link
Member

The semantics of having a monotonically increasing index with a negative offset is a bit confusing with the current API since the windows always begin from the start of the index.

That being said you can create your own custom definition of how the windows should be constructed: https://pandas.pydata.org/pandas-docs/stable/user_guide/computation.html#custom-window-rolling

So that being said, I'd be -1 on the idea given the ability to create a custom indexer but open to thoughts from others

@mroeschke mroeschke added Needs Discussion Requires discussion from core team before further action Window rolling, ewma, expanding and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 18, 2020
@yohplala
Copy link
Author

yohplala commented May 18, 2020

Hello Matthew,
Thanks for your reply.
I am mentioning another method taking a 'negative' window value. I think I have made a quiproquo, and I think I am referring to the shift function that has a period parameter that can be either positive or negative.

Matthew, yes, you are right and my demand can be addressed with already existing feature, except it seems to me that by playing on existing parameters, this should be supported by pandas.

I can propose an alternative, relying on existing parameters.
Couldn't the center parameter be also used for that, with values like right, center, and left?
I understand current False value being right, and True value being center.
Doc says:
By default, the result is set to the right edge of the window. This can be changed to the center of the window by setting center=True.
Why not extending so that result can be set to the left edge of the window?

@mroeschke
Copy link
Member

mroeschke commented Sep 11, 2020

Sorry for the long delay @yohplala.

This issue as actually brought up in #6772, where now you can reverse your original data, apply the rolling aggregation and then flip the result

In [12]: df
Out[12]:
                     value
time
2018-01-01 00:00:00    100
2018-01-01 00:00:01    101
2018-01-01 00:00:03    103
2018-01-01 00:00:04    111

In [13]: df[::-1].rolling('2s').sum()[::-1]
Out[13]:
                     value
time
2018-01-01 00:00:00  201.0
2018-01-01 00:00:01  101.0
2018-01-01 00:00:03  214.0
2018-01-01 00:00:04  111.0

I don't think we need a dedicated API for this operation so I'm going to close this for now

@mroeschke mroeschke changed the title ENH: Looking-forward rolling window ENH: Add VariableForwardWindowIndexer Sep 11, 2020
@mroeschke mroeschke removed the Needs Discussion Requires discussion from core team before further action label Sep 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Window rolling, ewma, expanding
Projects
None yet
Development

No branches or pull requests

2 participants