<jupyter_start><jupyter_text>![title](img/cover4.png) Copyright! This material is protected, please do not copy or distribute.    by:<NAME> ***Udemy course : Python Bootcamp for Data Science 2021 Numpy Pandas & Seaborn ***  14.5 Shifting Data Through Time (Lagging and Leading) First we import pandas library:<jupyter_code>import pandas as pd<jupyter_output><empty_output><jupyter_text>Lets read a time series from a csv file using the function **pd.read_csv()**, we set the index column to be the date column using the argument **index_col**, and we convert the dates to datatime format using the argument **parse_dates = True**:<jupyter_code>TS = pd.read_csv('data/ex17.csv', index_col = 'date', parse_dates = True)
TS<jupyter_output><empty_output><jupyter_text>We can shift the price **one day** backward by using the function **shift()**:<jupyter_code>TS.shift(periods = 1)<jupyter_output><empty_output><jupyter_text>We can shift the price **two days** backward by using the function **shift()**:<jupyter_code>TS.shift(periods = 2)<jupyter_output><empty_output><jupyter_text>We can shift the price **three days** backward by using the function **shift()**:<jupyter_code>TS.shift(periods = 3)<jupyter_output><empty_output><jupyter_text>We do not need to write the name of the argument **(periods)**, we can just pass the number of periods like this:<jupyter_code>TS.shift(3)<jupyter_output><empty_output><jupyter_text>We can make a forward shift by using **minus** for the periods. Here we use **(-1)** to shift the data one day forward:<jupyter_code>TS.shift(-1)<jupyter_output><empty_output><jupyter_text>Usually when dealing with time series, we create a shifted data and attach it as a new column in the time series, like this:<jupyter_code>TS['lag1'] = TS['apple'].shift(1)
TS<jupyter_output><empty_output><jupyter_text>Shifting data in time series generates **missing values**, we can delete these missing values using the function **dropna()**, and to make the changes reflected in the original times series we use the argument **inplace = True**:<jupyter_code>TS.dropna(inplace = True)
TS<jupyter_output><empty_output><jupyter_text>For example we can calculate the daily percent change for stock prices using the **shift()** function like this:<jupyter_code>TS['percent_change'] = ( TS['apple'] / TS['apple'].shift(1) ) -1
TS<jupyter_output><empty_output><jupyter_text>Again here we can delete the missing values like this:<jupyter_code>TS.dropna(inplace = True)
TS<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11572226656025539, 'fraction_numerical': 0.007980845969672785, 'mean_word_length': 7.246710526315789, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '667663', 'license': 'MIT', 'n_tokens_mistral': 813, 'n_tokens_neox': 789, 'n_words': 260, 'path': '14.5 Shifting Data Through Time (Lagging and Leading).ipynb', 'repo_name': 'SiamakMushakhian/Numpy-Pandas-Seaborn', 'size': '2511'}