0
0
Pandasdata~20 mins

Date arithmetic with timedelta in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Date Arithmetic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Adding timedelta to a pandas Series of dates
What is the output of this code snippet?
Pandas
import pandas as pd
from datetime import timedelta

dates = pd.Series(pd.to_datetime(['2024-01-01', '2024-01-02', '2024-01-03']))
result = dates + timedelta(days=3)
print(result)
A
0   2024-01-04
1   2024-01-05
2   2024-01-06
dtype: datetime64[ns]
B
0   2024-01-01
1   2024-01-02
2   2024-01-03
dtype: datetime64[ns]
CTypeError: unsupported operand type(s) for +: 'Series' and 'timedelta.timedelta'
D
0   2024-01-04 00:00:00
1   2024-01-05 00:00:00
2   2024-01-06 00:00:00
dtype: datetime64[ns]
Attempts:
2 left
💡 Hint
Try adding a timedelta object directly to a pandas Series of datetime values.
data_output
intermediate
2:00remaining
Subtracting timedelta from a DataFrame column
Given this DataFrame, what is the output of subtracting 5 hours from the 'timestamp' column?
Pandas
import pandas as pd
from datetime import timedelta

df = pd.DataFrame({'timestamp': pd.to_datetime(['2024-06-01 12:00', '2024-06-01 15:00'])})
df['new_time'] = df['timestamp'] - timedelta(hours=5)
print(df['new_time'])
A
0   2024-06-01 07:00:00
1   2024-06-01 10:00:00
Name: new_time, dtype: datetime64[ns]
B
0   2024-06-01 17:00:00
1   2024-06-01 20:00:00
Name: new_time, dtype: datetime64[ns]
CTypeError: unsupported operand type(s) for -: 'Series' and 'timedelta.timedelta'
D
0   2024-06-01 12:00:00
1   2024-06-01 15:00:00
Name: new_time, dtype: datetime64[ns]
Attempts:
2 left
💡 Hint
Subtracting timedelta decreases the datetime values by the specified duration.
🔧 Debug
advanced
2:00remaining
Identify the error in timedelta addition to a DataFrame
What error does this code produce and why?
Pandas
import pandas as pd
from datetime import timedelta

df = pd.DataFrame({'date': ['2024-01-01', '2024-01-02']})
df['date'] = df['date'] + timedelta(days=1)
print(df)
ATypeError: unsupported operand type(s) for +: 'Series' and 'timedelta.timedelta'
BValueError: could not convert string to datetime
CNo error, dates incremented by 1 day
DAttributeError: 'list' object has no attribute 'dt'
Attempts:
2 left
💡 Hint
Check the data type of the 'date' column before adding timedelta.
🚀 Application
advanced
2:00remaining
Calculate the difference in days between two date columns
Given this DataFrame, which option correctly creates a new column 'diff_days' showing the number of days between 'end_date' and 'start_date'?
Pandas
import pandas as pd

df = pd.DataFrame({
    'start_date': pd.to_datetime(['2024-01-01', '2024-01-10']),
    'end_date': pd.to_datetime(['2024-01-05', '2024-01-15'])
})
A
df['diff_days'] = df['end_date'] - df['start_date']
print(df['diff_days'])
B
df['diff_days'] = (df['end_date'] - df['start_date']).dt.days
print(df['diff_days'])
C
df['diff_days'] = (df['start_date'] - df['end_date']).days
print(df['diff_days'])
D
df['diff_days'] = (df['end_date'] + df['start_date']).dt.days
print(df['diff_days'])
Attempts:
2 left
💡 Hint
Subtracting datetime columns gives a timedelta Series; use .dt.days to get integer days.
🧠 Conceptual
expert
2:00remaining
Understanding timedelta behavior with mixed frequency data
You have a pandas Series with timestamps at irregular intervals. You add a timedelta of 1 day to this Series. What happens to the timestamps that fall on daylight saving time change days?
AA ValueError is raised because timedelta cannot handle daylight saving changes.
BThe timestamps shift by 23 or 25 hours depending on the daylight saving change.
CThe timestamps shift by exactly 24 hours regardless of daylight saving changes.
DThe timestamps remain unchanged because timedelta ignores irregular intervals.
Attempts:
2 left
💡 Hint
Timedelta represents a fixed duration, not calendar-aware shifts.