From the course: Data Analysis with Python and Pandas
Unlock this course with a free trial
Join today to access over 25,300 courses taught by industry experts.
Shifting time series
From the course: Data Analysis with Python and Pandas
Shifting time series
- [Instructor] We can shift a series by a specified number of rows using the shift method. And note that this will work with non-time series columns and non-time series data. This is particularly helpful in working with time series to compare values against previous periods. So here we have our dates series. We have a bunch of dates in our index, and then we have sales values in our series. If we shift this series without an argument passed, we will shift this series down by one. So our value on January 1st is now on January 2nd. Our value on January 2nd is now on February 1st, and so on. We'll shift this entire series down by one row. If we wanted to shift back one row, we could use shift negative one. And so this will shift forward by one row by default, but we can specify any positive or negative integer to shift the series by. And so here's an example of using shift to calculate a growth rate. We're dividing our sales by the sales in the prior row. We're subtracting one…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
-
(Locked)
Times in Python and pandas3m 8s
-
(Locked)
Converting to datetimes6m 16s
-
(Locked)
Formatting dates5m 20s
-
(Locked)
Date and time parts3m 4s
-
(Locked)
Challenge: pandas datetime basics1m 23s
-
(Locked)
Solution: pandas datetime basics2m 10s
-
(Locked)
Time deltas and arithmetic6m 54s
-
(Locked)
Challenge: Time deltas1m 10s
-
(Locked)
Solution: Time deltas1m 29s
-
(Locked)
Time series indices3m 58s
-
(Locked)
Missing time series data4m 45s
-
(Locked)
Challenge: Missing time series data1m 44s
-
(Locked)
Solution: Missing time series data2m 13s
-
(Locked)
Shifting time series3m 16s
-
(Locked)
Pro tip: diff()2m 54s
-
(Locked)
Challenge: shift() and diff()1m 39s
-
(Locked)
Solution: shift() and diff()2m 47s
-
(Locked)
Aggregation and resampling4m 6s
-
(Locked)
Challenge: Resampling41s
-
(Locked)
Solution: Resampling1m 53s
-
(Locked)
Rolling aggregations4m 35s
-
(Locked)
Challenge: Rolling aggregations45s
-
(Locked)
Solution: Rolling aggregations55s
-
(Locked)
Key takeaways1m 37s
-
(Locked)
-
-
-