0
0
Data Analysis Pythondata~10 mins

Date arithmetic (Timedelta) in Data Analysis Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Date arithmetic (Timedelta)
Start with two dates
Calculate difference: date2 - date1
Result is Timedelta object
Use Timedelta for addition/subtraction
Get days, seconds, or total_seconds
Apply result in analysis or display
We start with two dates, subtract to get a Timedelta, then use it to find differences or add/subtract time.
Execution Sample
Data Analysis Python
import pandas as pd

date1 = pd.to_datetime('2024-01-01')
date2 = pd.to_datetime('2024-01-10')
delta = date2 - date1
print(delta.days)
This code calculates the number of days between two dates using Timedelta.
Execution Table
StepActiondate1date2Timedelta (delta)Output
1Convert '2024-01-01' to datetime2024-01-01
2Convert '2024-01-10' to datetime2024-01-012024-01-10
3Calculate delta = date2 - date12024-01-012024-01-109 days 00:00:00
4Print delta.days2024-01-012024-01-109 days 00:00:009
💡 Finished calculation and printed the number of days difference.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
date1None2024-01-01 00:00:002024-01-01 00:00:002024-01-01 00:00:002024-01-01 00:00:00
date2NoneNone2024-01-10 00:00:002024-01-10 00:00:002024-01-10 00:00:00
deltaNoneNoneNone9 days 00:00:009 days 00:00:00
delta.daysNoneNoneNone99
Key Moments - 3 Insights
Why does subtracting two dates give a Timedelta instead of a number?
Subtracting two dates returns a Timedelta object that holds days and time difference, not just a number. See execution_table step 3 where delta is '9 days 00:00:00'.
How do we get just the number of days from the Timedelta?
Use the .days attribute of the Timedelta object, as shown in execution_table step 4 where delta.days is 9.
Can we add a Timedelta to a date to get a new date?
Yes, adding a Timedelta to a date shifts the date by that amount. This is a common use of Timedelta in date arithmetic.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of delta?
A1 day 00:00:00
B10 days 00:00:00
C9 days 00:00:00
DNone
💡 Hint
Check the 'Timedelta (delta)' column at step 3 in the execution_table.
At which step is the number of days difference printed?
AStep 3
BStep 4
CStep 2
DStep 1
💡 Hint
Look at the 'Output' column in the execution_table for when delta.days is printed.
If date2 was '2024-01-05' instead of '2024-01-10', what would delta.days be at step 3?
A4
B5
C9
D10
💡 Hint
Calculate difference between '2024-01-05' and '2024-01-01' as days.
Concept Snapshot
Date arithmetic uses Timedelta objects.
Subtracting two dates gives a Timedelta.
Timedelta holds days, seconds, microseconds.
Use .days to get whole days.
Add/subtract Timedelta to shift dates.
Useful for measuring time differences.
Full Transcript
This visual execution shows how to do date arithmetic using Timedelta in Python with pandas. We start by converting two date strings into datetime objects. Then we subtract one date from the other to get a Timedelta object representing the difference. This Timedelta shows how many days and time units separate the two dates. We can access the number of days using the .days attribute. This lets us easily find how many days passed between two dates. Timedelta can also be added or subtracted from dates to move forward or backward in time. The step-by-step table tracks variables and outputs, helping beginners see how the values change. Key moments clarify common confusions about why subtraction returns Timedelta and how to extract days. The quiz tests understanding by asking about values at specific steps and hypothetical changes. This approach helps learners see date arithmetic as simple, stepwise operations with clear results.