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
#6
(Feb-22-2020, 11:30 PM)new_to_python Wrote: Usually, when do people use the function shift()?
In many cases, when you need to compute differences of neighboring values in the array,
you can just use indexing, e.g.
import numpy as np
x = np.array([1,2,3,4,5,6])
res = (x[1:] - x[:-1])/x[:-1] 
The .shift method might be helpful e.g. when working with dates:
import pandas as pd 
index = pd.date_range('01 / 01 / 2020', periods = 5, freq ='10H') 
df = pd.DataFrame({"A":[1, 2, 3, 4, 5],  
                   "B":[10, 20, 30, 40, 50]},
                    index=index)
df
Output:
A B 2020-01-01 00:00:00 1 10 2020-01-01 10:00:00 2 20 2020-01-01 20:00:00 3 30 2020-01-02 06:00:00 4 40 2020-01-02 16:00:00 5 50
df.shift(freq="5H")
Output:
A B 2020-01-01 05:00:00 1 10 2020-01-01 15:00:00 2 20 2020-01-02 01:00:00 3 30 2020-01-02 11:00:00 4 40 2020-01-02 21:00:00 5 50
If freq parameter is specified, and index is of date/datetime type, this method shifts index values only.

Finally, sometimes it is easier to type df.shift(10) and get shifted data instead of using index-based version, e.g. something like this df.values[10:, ...].
Reply


Messages In This Thread
RE: Using shift to compute the percent change in a time series - by scidam - Feb-23-2020, 12:07 PM

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