Challenge - 5 Problems
Date Range Highlighting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of date range highlighting with matplotlib
What will be the output of this code snippet that highlights a date range on a line plot?
Matplotlib
import matplotlib.pyplot as plt import pandas as pd import numpy as np dates = pd.date_range('2024-01-01', periods=10) values = np.arange(10) plt.plot(dates, values) plt.axvspan(pd.Timestamp('2024-01-03'), pd.Timestamp('2024-01-06'), color='yellow', alpha=0.3) plt.show()
Attempts:
2 left
💡 Hint
Look at the function plt.axvspan and the dates it uses.
✗ Incorrect
The plt.axvspan function highlights a vertical span between two x-axis values. Here, it highlights from 2024-01-03 to 2024-01-06 with a yellow transparent color.
❓ data_output
intermediate1:30remaining
Number of points inside highlighted date range
Given the following data and highlighted date range, how many data points fall inside the highlighted range?
Matplotlib
import pandas as pd import numpy as np dates = pd.date_range('2024-01-01', periods=15) values = np.random.rand(15) data = pd.DataFrame({'date': dates, 'value': values}) highlight_start = pd.Timestamp('2024-01-05') highlight_end = pd.Timestamp('2024-01-10') inside_points = data[(data['date'] >= highlight_start) & (data['date'] <= highlight_end)] print(len(inside_points))
Attempts:
2 left
💡 Hint
Count dates from Jan 5 to Jan 10 inclusive.
✗ Incorrect
Dates from 2024-01-05 to 2024-01-10 inclusive are 6 days, so 6 points fall inside the range.
❓ visualization
advanced2:30remaining
Identify the correct highlighted date range in plot
Which option shows the correct code to highlight the date range from 2024-02-10 to 2024-02-15 on a time series plot?
Matplotlib
import matplotlib.pyplot as plt import pandas as pd import numpy as np dates = pd.date_range('2024-02-01', periods=20) values = np.random.randn(20).cumsum() plt.plot(dates, values) # Highlight code here plt.show()
Attempts:
2 left
💡 Hint
Use vertical span to highlight date ranges on x-axis.
✗ Incorrect
plt.axvspan highlights a vertical band between two x-axis dates. axhspan highlights horizontal bands (y-axis).
🔧 Debug
advanced2:00remaining
Identify the error in date range highlighting code
What error will this code produce when trying to highlight a date range?
Matplotlib
import matplotlib.pyplot as plt import pandas as pd import numpy as np dates = pd.date_range('2024-03-01', periods=10) values = np.arange(10) plt.plot(dates, values) plt.axvspan('2024-03-03', '2024-03-06', color='blue', alpha=0.3) plt.show()
Attempts:
2 left
💡 Hint
Check the types passed to plt.axvspan for date values.
✗ Incorrect
plt.axvspan expects numeric or datetime objects for x-axis limits. Passing strings causes a ValueError during internal conversion to float.
🚀 Application
expert3:00remaining
Calculate total duration of multiple highlighted date ranges
Given multiple highlighted date ranges, what is the total number of days covered without counting overlaps twice?
Matplotlib
import pandas as pd ranges = [ (pd.Timestamp('2024-04-01'), pd.Timestamp('2024-04-05')), (pd.Timestamp('2024-04-04'), pd.Timestamp('2024-04-10')), (pd.Timestamp('2024-04-12'), pd.Timestamp('2024-04-15')) ] # Calculate total days covered by these ranges without double counting overlaps
Attempts:
2 left
💡 Hint
Merge overlapping intervals before summing durations.
✗ Incorrect
The first two ranges overlap from Apr 4 to Apr 5. Merging them gives Apr 1 to Apr 10 (10 days). The third range is Apr 12 to Apr 15 (4 days). Total is 10 + 4 = 14 days, but since end dates are inclusive, days count is (10 - 1 + 1) + (15 - 12 + 1) = 10 + 4 = 14 days. However, careful calculation shows Apr 1 to Apr 10 is 10 days, Apr 12 to Apr 15 is 4 days, total 14 days. So correct answer is 14.