Python Tutorial: Lags, changes, and returns for stock price series
Key Takeaways
The video demonstrates how to manipulate time series data in Python using pandas, specifically calculating lags, changes, and returns for stock price series. It covers methods such as shift, diff, and percent_change to compare values at different points in time.
Full Transcript
in this video you will begin to manipulate time series data in particular you will learn how to move your data across time so that they can compare values at different points in time this involves shifting values into the future or creating lags by moving data into the past you will also learn how to calculate changes between values at different points in time lastly you will see how to calculate the change between values in percentage terms also call the rate of growth panelist has built-in methods for these calculations that leverage the day time index you learned about in the last segment let's again import a recent stock price time series for google you can let the read CSV function to the date parsing for you instead of using the two day time function you can tell read CSV to parse certain columns as dates just provide one or more target labels as a list you can also let read CSV cell a column as indexed by providing the index called parameter as a result you get a properly formatted time series your first time series method is dot shift it allows you to move all data in a series or data frame into the past or future the shifted version of the stock price has all prices moved by one period into the future as a result the first value in the series is not missing in contrast the lagged version of the stock price is moved one period into the past in this case the last value is now missing to shift data into the past use negative period numbers shifting data is useful to compare data at different points in time you can for instance calculate the rate of change from period to period which is also called financial return in finance the method div allows you not only to divide a series by a value but by an entire series for instance by another column in the same data frame partners make sure the dates for both series match up and we'll divide the aligned values accordingly as a result you get the relative change from the last period for every price that is the factor by which you need to multiply the last price to get the current price as you have seen before you can chain all data frame methods that return the data frame the return data frame will be used as input for the next calculation here we are subtracting 1 and multiplying the result by 100 to obtain the relative change in percentage terms another time series method is diff which calculates the change between values at different points in time by default the diversion of the close price is the difference in value since the last day stocks were traded you can use this information to also calculate one period returns just divide the absolute change by the shifted price and then multiply by 100 to get the same result as before finally since it is such a common operation ponders has a built-in method for you to calculate the percent change directly just select the column and call percent change multiply by 100 to get the same result as before all these methods have a periods keyword that you have already seen for shift and their defaults to the value 1 if you provide a higher value you can calculate returns for data points several periods apart as in this example for prices 3 trading days apart now let's practice this new time series method
Original Description
Want to learn more? Take the full course at https://learn.datacamp.com/courses/manipulating-time-series-data-in-python at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
In this video, you will begin to manipulate time series data.
In particular, you will learn how to move your data across time so that you can compare values at different points in time.
This involves shifting values into the future, or creating lags by moving data into the past.
You will also learn how to calculate changes between values at different points in time.
Lastly, you will see how to calculate the change between values in percentage terms, also called the rate of growth.
Pandas has builtin methods for these calculations that leverage the DateTimeIndex you learned about in the last segment.
Let's again import a recent stock price time series for Google.
You can let the read_csv function do the date parsing for you.
Instead of using the to_datetime function, you can tell read_csv to parse certain columns as dates ' just provide one or more target labels as a list. You can also let read_csv set a column as index by providing the index_col parameter.
As a result, you get a properly formatted time series.
Your first time series method is dot-shift.
It allows you to move all data in a Series or DataFrame into the past or future.
The 'shifted' version of the stock price has all prices moved by 1 period into the future.
As a result, the first value in the series is now missing.
In contrast, the lagged version of the stock price is moved 1 period into the past. In this case, the last value is now missing.
To shift data into the past, use negative period numbers.
Shifting data is useful to compare data at different points in time.
You can, for instance, calculate the rate of change from period to period, which is also called financial return in finance.
The method dot-div allows you not only to divide a Series by a value, bu
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from DataCamp · DataCamp · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
SQL Server Tutorial: Date manipulation
DataCamp
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
R Tutorial: Adding aesthetics to represent a variable
DataCamp
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
Python Tutorial: Preparation for modeling
DataCamp
Python Tutorial: Machine Learning modeling steps
DataCamp
R Tutorial: The prior model
DataCamp
R Tutorial: Data & the likelihood
DataCamp
R Tutorial: The posterior model
DataCamp
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
DataCamp
R Tutorial: Bivariate graphics
DataCamp
Python Tutorial: Customer Segmentation in Python
DataCamp
Python Tutorial: Time cohorts
DataCamp
Python Tutorial: Calculate cohort metrics
DataCamp
Python Tutorial: Cohort analysis visualization
DataCamp
R Tutorial: Building Dashboards with flexdashboard
DataCamp
R Tutorial: Anatomy of a flexdashboard
DataCamp
R Tutorial: Layout basics
DataCamp
R Tutorial: Advanced layouts
DataCamp
Python Tutorial: Time Series Analysis in Python
DataCamp
Python Tutorial: Correlation of Two Time Series
DataCamp
Python Tutorial: Simple Linear Regressions
DataCamp
Python Tutorial: Autocorrelation
DataCamp
R Tutorial: The gapminder dataset
DataCamp
R Tutorial: The filter verb
DataCamp
R Tutorial: The arrange verb
DataCamp
R Tutorial: The mutate verb
DataCamp
R Tutorial: What is cluster analysis?
DataCamp
R Tutorial: Distance between two observations
DataCamp
R Tutorial: The importance of scale
DataCamp
R Tutorial: Measuring distance for categorical data
DataCamp
Python Tutorial: Plotting multiple graphs
DataCamp
Python Tutorial: Customizing axes
DataCamp
Python Tutorial: Legends, annotations, & styles
DataCamp
Python Tutorial: Introduction to iterators
DataCamp
Python Tutorial: Playing with iterators
DataCamp
Python Tutorial: Using iterators to load large files into memory
DataCamp
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
SQL Tutorial: Tables: At the core of every database
DataCamp
SQL Tutorial: Update your database as the structure changes
DataCamp
Python Tutorial: Classification-Tree Learning
DataCamp
Python Tutorial: Decision-Tree for Classification
DataCamp
Python Tutorial: Decision-Tree for Regression
DataCamp
Python Tutorial: Census Subject Tables
DataCamp
Python Tutorial: Census Geography
DataCamp
Python Tutorial: Using the Census API
DataCamp
R Tutorial: A/B Testing in R
DataCamp
R Tutorial: Baseline Conversion Rates
DataCamp
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
R Tutorial: Introduction to qualitative data
DataCamp
R Tutorial: Understanding your qualitative variables
DataCamp
R Tutorial: Making Better Plots
DataCamp
SQL Tutorial: OLTP and OLAP
DataCamp
SQL Tutorial: Storing data
DataCamp
SQL Tutorial: Database design
DataCamp
Python Tutorial: Introduction to spaCy
DataCamp
Python Tutorial: Statistical Models
DataCamp
Python Tutorial: Rule-based Matching
DataCamp
More on: Data Literacy
View skill →
🎓
Tutor Explanation
DeepCamp AI