Cumulative sum vs .diff()
In the video, you have learned about expanding windows that allow you to run cumulative calculations.
The cumulative sum method has in fact the opposite effect of the .diff()
method that you came across in chapter 1.
To illustrate this, let's use the Google stock price time series, create the differences between prices, and reconstruct the series using the cumulative sum.
This is a part of the course
“Manipulating Time Series Data in Python”
Exercise instructions
We have already imported pandas
as pd
and matplotlib.pyplot
as plt
. We have also loaded google stock prices into the variable data
- Apply
.diff()
todata
, drop missing values, and assign the result todifferences
. - Use
.first('D')
to select the first price fromdata
, and assign it tostart_price
. - Use
.append()
to combinestart_price
anddifferences
, apply.cumsum()
and assign this tocumulative_sum
. - Use
.equals()
to comparedata
andcumulative_sum
, and print the result.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Calculate differences
differences = ____
# Select start price
start_price = ____
# Calculate cumulative sum
cumulative_sum = ____
# Validate cumulative sum equals data
print(____)
This exercise is part of the course
Manipulating Time Series Data in Python
In this course you'll learn the basics of working with time series data.
This chapter will show you how to use window function to calculate time series metrics for both rolling and expanding windows.
Exercise 1: Rolling window functions with pandasExercise 2: Rolling average air quality since 2010 for new york cityExercise 3: Rolling 360-day median & std. deviation for nyc ozone data since 2000Exercise 4: Rolling quantiles for daily air quality in nycExercise 5: Expanding window functions with pandasExercise 6: Cumulative sum vs .diff()Exercise 7: Cumulative return on $1,000 invested in google vs apple IExercise 8: Cumulative return on $1,000 invested in google vs apple IIExercise 9: Case study: S&P500 price simulationExercise 10: Random walk IExercise 11: Random walk IIExercise 12: Random walk IIIExercise 13: Relationships between time series: correlationExercise 14: Annual return correlations among several stocksWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.