Python Tutorial: Lags, changes, and returns for stock price series

DataCamp · Beginner ·🛠️ AI Tools & Apps ·6y ago

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 SQL Server Tutorial: Date manipulation
SQL Server Tutorial: Date manipulation
DataCamp
2 R Tutorial: Intermediate Interactive Data Visualization with plotly in R
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
3 R Tutorial: Adding aesthetics to represent a variable
R Tutorial: Adding aesthetics to represent a variable
DataCamp
4 R Tutorial: Moving Beyond Simple Interactivity
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
5 Python Tutorial: Why use ML for marketing? Strategies and use cases
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
6 Python Tutorial: Preparation for modeling
Python Tutorial: Preparation for modeling
DataCamp
7 Python Tutorial: Machine Learning modeling steps
Python Tutorial: Machine Learning modeling steps
DataCamp
8 R Tutorial: The prior model
R Tutorial: The prior model
DataCamp
9 R Tutorial: Data & the likelihood
R Tutorial: Data & the likelihood
DataCamp
10 R Tutorial: The posterior model
R Tutorial: The posterior model
DataCamp
11 R Tutorial: An Introduction to plotly
R Tutorial: An Introduction to plotly
DataCamp
12 R Tutorial: Plotting a single variable
R Tutorial: Plotting a single variable
DataCamp
13 R Tutorial: Bivariate graphics
R Tutorial: Bivariate graphics
DataCamp
14 Python Tutorial: Customer Segmentation in Python
Python Tutorial: Customer Segmentation in Python
DataCamp
15 Python Tutorial: Time cohorts
Python Tutorial: Time cohorts
DataCamp
16 Python Tutorial: Calculate cohort metrics
Python Tutorial: Calculate cohort metrics
DataCamp
17 Python Tutorial: Cohort analysis visualization
Python Tutorial: Cohort analysis visualization
DataCamp
18 R Tutorial: Building Dashboards with flexdashboard
R Tutorial: Building Dashboards with flexdashboard
DataCamp
19 R Tutorial: Anatomy of a flexdashboard
R Tutorial: Anatomy of a flexdashboard
DataCamp
20 R Tutorial: Layout basics
R Tutorial: Layout basics
DataCamp
21 R Tutorial: Advanced layouts
R Tutorial: Advanced layouts
DataCamp
22 Python Tutorial: Time Series Analysis in Python
Python Tutorial: Time Series Analysis in Python
DataCamp
23 Python Tutorial: Correlation of Two Time Series
Python Tutorial: Correlation of Two Time Series
DataCamp
24 Python Tutorial: Simple Linear Regressions
Python Tutorial: Simple Linear Regressions
DataCamp
25 Python Tutorial: Autocorrelation
Python Tutorial: Autocorrelation
DataCamp
26 R Tutorial: The gapminder dataset
R Tutorial: The gapminder dataset
DataCamp
27 R Tutorial: The filter verb
R Tutorial: The filter verb
DataCamp
28 R Tutorial: The arrange verb
R Tutorial: The arrange verb
DataCamp
29 R Tutorial: The mutate verb
R Tutorial: The mutate verb
DataCamp
30 R Tutorial: What is cluster analysis?
R Tutorial: What is cluster analysis?
DataCamp
31 R Tutorial: Distance between two observations
R Tutorial: Distance between two observations
DataCamp
32 R Tutorial: The importance of scale
R Tutorial: The importance of scale
DataCamp
33 R Tutorial: Measuring distance for categorical data
R Tutorial: Measuring distance for categorical data
DataCamp
34 Python Tutorial: Plotting multiple graphs
Python Tutorial: Plotting multiple graphs
DataCamp
35 Python Tutorial: Customizing axes
Python Tutorial: Customizing axes
DataCamp
36 Python Tutorial: Legends, annotations, & styles
Python Tutorial: Legends, annotations, & styles
DataCamp
37 Python Tutorial: Introduction to iterators
Python Tutorial: Introduction to iterators
DataCamp
38 Python Tutorial: Playing with iterators
Python Tutorial: Playing with iterators
DataCamp
39 Python Tutorial: Using iterators to load large files into memory
Python Tutorial: Using iterators to load large files into memory
DataCamp
40 SQL Tutorial: Introduction to Relational Databases in SQL
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
41 SQL Tutorial: Tables: At the core of every database
SQL Tutorial: Tables: At the core of every database
DataCamp
42 SQL Tutorial: Update your database as the structure changes
SQL Tutorial: Update your database as the structure changes
DataCamp
43 Python Tutorial: Classification-Tree Learning
Python Tutorial: Classification-Tree Learning
DataCamp
44 Python Tutorial: Decision-Tree for Classification
Python Tutorial: Decision-Tree for Classification
DataCamp
45 Python Tutorial: Decision-Tree for Regression
Python Tutorial: Decision-Tree for Regression
DataCamp
46 Python Tutorial: Census Subject Tables
Python Tutorial: Census Subject Tables
DataCamp
47 Python Tutorial: Census Geography
Python Tutorial: Census Geography
DataCamp
48 Python Tutorial: Using the Census API
Python Tutorial: Using the Census API
DataCamp
49 R Tutorial: A/B Testing in R
R Tutorial: A/B Testing in R
DataCamp
50 R Tutorial: Baseline Conversion Rates
R Tutorial: Baseline Conversion Rates
DataCamp
51 R Tutorial: Designing an Experiment - Power Analysis
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
52 R Tutorial: Introduction to qualitative data
R Tutorial: Introduction to qualitative data
DataCamp
53 R Tutorial: Understanding your qualitative variables
R Tutorial: Understanding your qualitative variables
DataCamp
54 R Tutorial: Making Better Plots
R Tutorial: Making Better Plots
DataCamp
55 SQL Tutorial: OLTP and OLAP
SQL Tutorial: OLTP and OLAP
DataCamp
56 SQL Tutorial: Storing data
SQL Tutorial: Storing data
DataCamp
57 SQL Tutorial: Database design
SQL Tutorial: Database design
DataCamp
58 Python Tutorial: Introduction to spaCy
Python Tutorial: Introduction to spaCy
DataCamp
59 Python Tutorial: Statistical Models
Python Tutorial: Statistical Models
DataCamp
60 Python Tutorial: Rule-based Matching
Python Tutorial: Rule-based Matching
DataCamp

This video teaches how to manipulate time series data in Python using pandas, covering methods to calculate lags, changes, and returns for stock price series. It provides hands-on coding experience and practical applications for daily work.

Key Takeaways
  1. Import a stock price time series using read_csv
  2. Use the shift method to move data into the past or future
  3. Calculate changes between values using the diff method
  4. Compute returns using the percent_change method
  5. Chain data frame methods to perform multiple calculations
  6. Use the periods keyword to calculate returns for data points several periods apart
💡 The pandas library provides built-in methods for time series calculations, making it easy to manipulate and analyze financial data.

Related Reads

Up next
Wix AI Tools Explained: Complete 2026 Guide
TheFigCo
Watch →