0
0
Pandasdata~10 mins

Date arithmetic with timedelta in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Date arithmetic with timedelta
Start with a date
Create timedelta (days, hours, etc.)
Add or subtract timedelta from date
Get new date result
Use new date for analysis or display
We start with a date, create a time difference (timedelta), then add or subtract it to get a new date.
Execution Sample
Pandas
import pandas as pd
start_date = pd.Timestamp('2024-06-01')
delta = pd.Timedelta(days=5)
new_date = start_date + delta
print(new_date)
This code adds 5 days to June 1, 2024, and prints the new date.
Execution Table
StepVariableValueOperationResult
1start_date2024-06-01 00:00:00Assign date2024-06-01 00:00:00
2delta5 days 00:00:00Create timedelta5 days 00:00:00
3new_dateN/AAdd start_date + delta2024-06-06 00:00:00
4print(new_date)N/AOutput new_date2024-06-06 00:00:00
💡 Completed adding timedelta to start_date and printed the result.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
start_dateN/A2024-06-01 00:00:002024-06-01 00:00:002024-06-01 00:00:00
deltaN/A5 days 00:00:005 days 00:00:005 days 00:00:00
new_dateN/AN/A2024-06-06 00:00:002024-06-06 00:00:00
Key Moments - 2 Insights
Why does adding timedelta to a date give a new date instead of a number?
Adding timedelta shifts the date by that time amount, producing a new date object, as shown in step 3 of the execution_table.
Can timedelta represent negative time to subtract days?
Yes, timedelta can be negative. Adding a negative timedelta subtracts days, similar to step 3 but with a negative value.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of new_date?
A2024-06-01 00:00:00
B2024-06-06 00:00:00
C5 days 00:00:00
DN/A
💡 Hint
Check the 'Result' column for step 3 in the execution_table.
If delta was pd.Timedelta(days=-3), what would new_date be after step 3?
A2024-05-29 00:00:00
B2024-06-04 00:00:00
C2024-06-01 00:00:00
D2024-06-08 00:00:00
💡 Hint
Subtracting 3 days from June 1, 2024 moves the date back to May 29, 2024.
According to variable_tracker, what is the value of delta after step 2?
AN/A
B2024-06-01 00:00:00
C5 days 00:00:00
D2024-06-06 00:00:00
💡 Hint
Look at the 'After Step 2' column for delta in variable_tracker.
Concept Snapshot
Date arithmetic with timedelta in pandas:
- Use pd.Timestamp for dates
- Use pd.Timedelta for time differences
- Add or subtract timedelta to dates
- Result is a new date shifted by timedelta
- Supports days, hours, minutes, seconds
Full Transcript
This visual execution shows how to add a time difference to a date using pandas. We start with a date called start_date set to June 1, 2024. Then we create a timedelta of 5 days. Adding this timedelta to start_date gives a new date, June 6, 2024. The execution table traces each step, showing variable values and operations. The variable tracker shows how start_date, delta, and new_date change over time. Key moments clarify why adding timedelta results in a new date and that timedelta can be negative to subtract days. The quiz tests understanding of these steps and values. This helps beginners see exactly how date arithmetic works in pandas.