Python Forum
Using shift to compute the percent change in a time series
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using shift to compute the percent change in a time series
#1
Hi, somebody mentioned that one could use ts/ts.shift(1)-1 to calculate the percent changes in a time series. I tried to understand this by hand calculating the percent changes on 2000-04-30. I got -3.1619163378849615 instead of -1.462553 using the formula. Have I done something wrong? I though percent changes is (new value - old value)/old value *100%.

In [91]: ts                                                                                                         
Out[91]: 
2000-01-31   -0.047169
2000-02-29    0.636350
2000-03-31    1.207707
2000-04-30   -0.558628
Freq: M, dtype: float64

In [92]: ts.shift(1)                                                                                                
Out[92]: 
2000-01-31         NaN
2000-02-29   -0.047169
2000-03-31    0.636350
2000-04-30    1.207707
Freq: M, dtype: float64

In [93]: ts/ts.shift(1)-1                                                                                           
Out[93]: 
2000-01-31          NaN
2000-02-29   -14.490780
2000-03-31     0.897866
2000-04-30    -1.462553
Freq: M, dtype: float64

In [94]:                                                                                                            

In [94]: (1.207707+0.558628)/(-0.558628)                                                                            
Out[94]: -3.1619163378849615
Reply


Messages In This Thread
Using shift to compute the percent change in a time series - by new_to_python - Feb-21-2020, 11:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help: Conversion of Electricity Data into Time Series Data SmallGuy 3 1,235 Oct-04-2023, 03:31 PM
Last Post: deanhystad
  Time Series Production Process Problem Mzarour 1 2,140 Feb-28-2023, 12:25 PM
Last Post: get2sid
  validate the existing value and change the series datahub 0 1,213 May-03-2022, 08:00 PM
Last Post: datahub
  reduce time series based on sum condition amdi40 0 1,109 Apr-06-2022, 09:09 AM
Last Post: amdi40
  How to accumulate volume of time series amdi40 3 2,318 Feb-15-2022, 02:23 PM
Last Post: amdi40
  pandas: Compute the % of the unique values in a column JaneTan 1 1,788 Oct-25-2021, 07:55 PM
Last Post: jefsummers
  Recommendations for ML libraries for time-series forecast AndreasPython 0 1,892 Jan-06-2021, 01:03 PM
Last Post: AndreasPython
  Combine groupby() and shift() in pandas rama27 0 4,223 Nov-17-2020, 09:49 PM
Last Post: rama27
  Pandas - compute means per category and time rama27 7 3,524 Nov-13-2020, 08:55 AM
Last Post: PsyPy
  Time Series forecating with multiple independent variables Krychol88 1 1,876 Oct-23-2020, 08:11 AM
Last Post: DPaul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020