Data Analysis with Python: From Raw Data to Real-World Insights
BOOK 2 · APPLIED ANALYSIS · CHAPTER 11

Chapter 11 exercises: Dates, Time and Change

On this page

Attempt these tasks before opening the worked answers. Use the chapter experiment notebook, or import NumPy as np and Pandas as pd in your own notebook. Retain your first attempt and explain each correction.

B2-11-E01 · Respect temporal gaps

Calculate percentage change only where neighbouring observations are consecutive years.

years=pd.Series([2019,2020,2022]);values=pd.Series([10.,12.,18.])
One hint after an attempt

Use the time difference as a validity mask.

B2-11-E02 · Keep an unresolved period

Resample daily and preserve an unresolved middle day, not zero.

s=pd.Series([3.,4.],index=pd.to_datetime(["2023-01-01","2023-01-03"]))
One hint after an attempt

Inspect the empty bin explicitly.

B2-11-E03 · Predict a trailing summary

Compute a three-observation trailing mean with complete windows only.

s=pd.Series([10.,20.,30.,40.])
One hint after an attempt

State which three values enter the first complete result.

Reflect and get feedback

Explain one result without looking at the code. Identify one boundary case that would change your method. An optional tutor prompt is: “Here is my attempt at B2-11-E03. Give one hint about the violated assumption; do not give the solution yet.” Then use the worked answers.

Teaching edition · verified experiments · PDF and EPUB editions not generated.