Consider time series for multiple days (eg. stock data in our case), we would like to graphically compare data from today (this month, this week) with data from yesterday (last month, last week) to plot them over each other to get some insights about trend shifts. Hence we need to shift the old data by time delta n datapoints ahead.
# install yahoo finance
# !pip3 install yfinance
import datetime
import pandas as pd
import matplotlib.pyplot as plt
# yahoo API
import yfinance as yf
ticker = 'FB' # META Platforms
start_time = datetime.datetime(2017, 10, 1)
#end_time = datetime.datetime(2019, 1, 20)
end_time = datetime.datetime.now().date().isoformat() # today
df = yf.download(ticker, start=start_time, end=end_time)
# use numerical integer index instead of date
df = df.reset_index()
[*********************100%***********************] 1 of 1 completed
Let's just plot the regular stock time series data so we have an ide what we are working with.
plt.figure(figsize=(15,7))
plt.title('Price chart (Adj Close) ' + ticker)
plt.plot(df['Adj Close'])
plt.show()
df.head(3)
Date | Open | High | Low | Close | Adj Close | Volume | |
---|---|---|---|---|---|---|---|
0 | 2017-10-02 | 171.389999 | 171.869995 | 168.750000 | 169.470001 | 169.470001 | 13392300 |
1 | 2017-10-03 | 169.300003 | 170.729996 | 169.199997 | 169.960007 | 169.960007 | 8190300 |
2 | 2017-10-04 | 169.830002 | 170.669998 | 168.289993 | 168.419998 | 168.419998 | 12127900 |
By shifting the data we can compare specific stage of time series with the same time series just period away. So let's say we have price graph for a month and we want to compare how the stock price looked like compared to previous month (some arbitrary period). We can plot them over each other to compare graphically, then we can say that stock performed this month better than previous month for example.
We will create new time shifted column with Close price in the same dataframe.
time_shift = 100 # number of datapoints that will be shifted
df['Shifted Close'] = df['Close'].shift(time_shift) # move this by n time datapoints
df.head(3) # quickly check the extended dataframe
Date | Open | High | Low | Close | Adj Close | Volume | Shifted Close | |
---|---|---|---|---|---|---|---|---|
0 | 2017-10-02 | 171.389999 | 171.869995 | 168.750000 | 169.470001 | 169.470001 | 13392300 | NaN |
1 | 2017-10-03 | 169.300003 | 170.729996 | 169.199997 | 169.960007 | 169.960007 | 8190300 | NaN |
2 | 2017-10-04 | 169.830002 | 170.669998 | 168.289993 | 168.419998 | 168.419998 | 12127900 | NaN |
Visualize both regular and time shifted time series for whole historical date range.
plt.figure(figsize=(15,7))
plt.title('Price chart (Close) ' + ticker)
plt.plot(df['Date'], df['Close'], label="non-shifted Close", alpha=0.99, linewidth=1) # last "day/period" for regular price,
plt.plot(df['Date'], df['Shifted Close'], label="shifted Close", alpha=0.5, linewidth=0.5) # just limits shifted lagging price ahead
# for the shifted time series, the time axis is the same, so we will keep it the same as the original time series
# and since the time shifted series is "one period" ahead, then we have to plot whole period before our period
plt.legend(loc='upper left')
plt.show()
Visualize both regular and time shifted time series for shifting period only. This might be rather useful for visualization of periodical time series.
plt.figure(figsize=(15,7))
plt.title('Price chart (Close) ' + ticker)
plt.plot(df['Date'][-time_shift:],
df['Close'][-time_shift:],
label="non-shifted Close",
alpha=0.99, linewidth=1) # last "day/period" for regular price
plt.plot(df['Date'][-time_shift:],
df['Shifted Close'][-time_shift:],
label="shifted Close",
alpha=0.5, linewidth=0.5) # limits shifted lagging price ahead
# for the shifted time series, the time axis is the same, so we will keep it the same as the original time series
# and since the time shifted series is one period ahead, then we have to plot whole period before our period
plt.legend(loc='upper left')
plt.show()
From the above plot we can clearly see trend differences for the past period/study time interval.