简体   繁体   中英

pandas series pct_change with initial expanding window

I need to calculate pct change for pd series with increasing window size till N followed by fixed size of N.

If I use pct_change with periods of N, I get initial nans.

s = pd.Series([1, 2, 4, 5, 6], name='Numbers')

s.pct_change(periods=3)

output

0    NaN
1    NaN
2    NaN
3    4.0
4    2.0
Name: Numbers, dtype: float64

I want to replace initial NaN's with

0    0   (1- 1) / 1
1    1  (2 - 1) / 1
2    3  (4 - 1)/ 1
3    4.0
4    2.0

So I want to have some kind of expanding window and replace initial NaNs with the new values. Any pointers would be appreciated.


EDIT..

adding one more example for clarity

s = pd.Series([4, 5, 6, 7, 8, 9], name='Numbers')

s.pct_change(periods=3)

output

0     NaN
1     NaN
2     NaN
3    0.75
4    0.60
5    0.50
Name: Numbers, dtype: float64

The output I expect

0     0
1     0.25  (5-4)/4
2     0.5   (6-4)/4
3    0.75
4    0.60
5    0.50

Is this what you want?

s = pd.Series([4, 5, 6, 7, 8, 9], name='Numbers')
s.pct_change(periods=3).fillna((s-s.iat[0])/s.iat[0])

Result

0    0.00
1    0.25
2    0.50
3    0.75
4    0.60
5    0.50

Try this:

This will fill the NaN values of your pct_change() function by iterating on an expanding series.

s.pct_change(3).fillna(pd.Series([(i.iloc[-1] - i.iloc[0])/i.iloc[0] for i in s.expanding()]))

or

s.pct_change(3).fillna(s.expanding().apply(lambda x: (x.iloc[-1] - x.iloc[0])/x.iloc[0]))

Output:

0    0.00
1    0.25
2    0.50
3    0.75
4    0.60
5    0.50
Name: Numbers, dtype: float64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM