Challenge - 5 Problems
Timedelta Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Calculate the difference in days between two dates
What is the output of this code snippet that calculates the difference in days between two dates?
Data Analysis Python
import pandas as pd start_date = pd.to_datetime('2024-01-01') end_date = pd.to_datetime('2024-01-15') diff = end_date - start_date print(diff.days)
Attempts:
2 left
💡 Hint
Remember that the difference between dates counts full days between them.
✗ Incorrect
The difference between January 15 and January 1 is 14 full days, so diff.days returns 14.
❓ data_output
intermediate2:00remaining
Add a timedelta to a date
What is the resulting date after adding 10 days to '2024-03-10'?
Data Analysis Python
import pandas as pd initial_date = pd.to_datetime('2024-03-10') new_date = initial_date + pd.Timedelta(days=10) print(new_date.strftime('%Y-%m-%d'))
Attempts:
2 left
💡 Hint
Adding 10 days means moving forward 10 calendar days.
✗ Incorrect
Adding 10 days to March 10 results in March 20, 2024.
❓ visualization
advanced3:00remaining
Plotting date ranges with timedelta
Which option correctly creates a plot showing dates from '2024-01-01' to '2024-01-10' with daily intervals?
Data Analysis Python
import pandas as pd import matplotlib.pyplot as plt dates = pd.date_range(start='2024-01-01', end='2024-01-10') values = range(len(dates)) plt.plot(dates, values) plt.xlabel('Date') plt.ylabel('Value') plt.title('Date Range Plot') plt.show()
Attempts:
2 left
💡 Hint
Check if the dates cover the full range and if the plot type matches the code.
✗ Incorrect
The code creates a line plot with dates from Jan 1 to Jan 10 on the x-axis and values 0 to 9 on the y-axis.
🧠 Conceptual
advanced2:30remaining
Understanding timedelta components
Given a timedelta of 3 days, 4 hours, and 30 minutes, what is the total number of seconds?
Data Analysis Python
from datetime import timedelta td = timedelta(days=3, hours=4, minutes=30) total_seconds = td.total_seconds() print(int(total_seconds))
Attempts:
2 left
💡 Hint
Calculate seconds for days, hours, and minutes separately then add.
✗ Incorrect
3 days = 259200 seconds, 4 hours = 14400 seconds, 30 minutes = 1800 seconds; total = 259200 + 14400 + 1800 = 275400 seconds.
🔧 Debug
expert2:00remaining
Identify the error in timedelta subtraction
What error does this code raise when subtracting a timedelta from a string date?
Data Analysis Python
from datetime import timedelta start = '2024-05-01' result = start - timedelta(days=5) print(result)
Attempts:
2 left
💡 Hint
Check the data types involved in the subtraction.
✗ Incorrect
You cannot subtract a timedelta directly from a string; the string must be converted to a date first.