📖 Scenario: You work in a small delivery company. You have a list of package shipment dates. You want to find out the expected delivery dates by adding a fixed number of days to each shipment date.
🎯 Goal: Build a pandas DataFrame with shipment dates, add a timedelta to calculate expected delivery dates, and display the results.
📋 What You'll Learn
Create a pandas DataFrame with shipment dates
Create a timedelta variable representing the delivery time
Add the timedelta to each shipment date to get expected delivery dates
Print the DataFrame showing shipment and expected delivery dates
💡 Why This Matters
🌍 Real World
Delivery companies often need to calculate expected delivery dates by adding fixed transit times to shipment dates.
💼 Career
Data analysts and scientists use date arithmetic to analyze timelines, schedules, and deadlines in many industries.
Progress0 / 4 steps
1
Create a pandas DataFrame with shipment dates
Import pandas as pd and create a DataFrame called shipments with a column 'shipment_date' containing these exact dates as strings: '2024-06-01', '2024-06-05', '2024-06-10'.
Pandas
Hint
Use pd.DataFrame with a dictionary where the key is 'shipment_date' and the value is the list of date strings.
2
Convert shipment_date to datetime and create timedelta
Convert the 'shipment_date' column in shipments to datetime type using pd.to_datetime(). Then create a variable called delivery_time that is a pd.Timedelta of 7 days.
Pandas
Hint
Use pd.to_datetime() to convert the column. Use pd.Timedelta(days=7) to create the timedelta.
3
Calculate expected delivery dates by adding timedelta
Create a new column in shipments called 'expected_delivery' by adding the delivery_time timedelta to the 'shipment_date' column.
Pandas
Hint
Use simple addition of the timedelta to the datetime column to create the new column.
4
Print the DataFrame with shipment and expected delivery dates
Print the shipments DataFrame to show the 'shipment_date' and 'expected_delivery' columns.
Pandas
Hint
Use print(shipments) to display the DataFrame.
Practice
(1/5)
1. What does pd.Timedelta(days=3) represent in pandas?
easy
A. A string describing 3 days
B. A specific date 3 days from now
C. A time difference of 3 days
D. A timestamp at 3 AM
Solution
Step 1: Understand Timedelta purpose
pd.Timedelta is used to represent a duration or difference in time, not a fixed date or time.
Step 2: Interpret the argument
The argument days=3 means the duration is 3 days long.
Final Answer:
A time difference of 3 days -> Option C
Quick Check:
Timedelta(days=3) = 3 days [OK]
Hint: Timedelta means duration, not a fixed date [OK]
Common Mistakes:
Thinking Timedelta is a fixed date
Confusing Timedelta with Timestamp
Assuming it returns a string
2. Which of the following is the correct way to add 5 days to a pandas Timestamp ts?
easy
A. ts.add(pd.Timedelta(5))
B. ts + pd.Timedelta('5')
C. ts + pd.Timedelta(5 days)
D. ts + pd.Timedelta(days=5)
Solution
Step 1: Check correct Timedelta syntax
The correct way to specify 5 days is pd.Timedelta(days=5). The argument must be named or a valid string.
Step 2: Verify addition syntax
Adding a Timedelta to a Timestamp uses the + operator, so ts + pd.Timedelta(days=5) is correct.
Final Answer:
ts + pd.Timedelta(days=5) -> Option D
Quick Check:
Correct syntax uses days=5 inside Timedelta [OK]
Hint: Use named argument days=5 inside Timedelta [OK]
pd.Timedelta(5) is missing the unit. It needs a keyword like days=5 or a string like '5 days'.
Step 2: Understand error cause
Without specifying units, pandas cannot interpret the number 5 alone, causing an error.
Final Answer:
Timedelta needs a unit like days=5 -> Option A
Quick Check:
Timedelta(5) missing units causes error [OK]
Hint: Always specify units when creating Timedelta [OK]
Common Mistakes:
Passing integer without units
Assuming default unit is days
Confusing Timestamp and Timedelta usage
5. You have a pandas DataFrame df with a column 'date' of type datetime. You want to create a new column 'date_plus_7' that adds 7 days to each date. Which code correctly does this?
hard
A. df['date_plus_7'] = df['date'] + pd.Timedelta('7 days')
B. df['date_plus_7'] = df['date'] + pd.Timedelta(days=7)
C. df['date_plus_7'] = df['date'] + pd.Timedelta(7)
D. df['date_plus_7'] = df['date'] + pd.Timedelta(days=7) * len(df)
Solution
Step 1: Understand Timedelta broadcasting
Adding a Timedelta to a pandas Series of dates works if the Timedelta is a scalar (single duration).
Step 2: Check each option
df['date_plus_7'] = df['date'] + pd.Timedelta('7 days') uses a string '7 days' which works but is less common in vectorized operations. df['date_plus_7'] = df['date'] + pd.Timedelta(days=7) uses pd.Timedelta(days=7) which is the standard and recommended way. df['date_plus_7'] = df['date'] + pd.Timedelta(7) misses units and causes error. df['date_plus_7'] = df['date'] + pd.Timedelta(days=7) * len(df) multiplies timedelta by length, which incorrectly scales the timedelta.
Step 3: Choose the best practice
df['date_plus_7'] = df['date'] + pd.Timedelta(days=7) is the correct and recommended way to add 7 days to each date in a column.
Final Answer:
df['date_plus_7'] = df['date'] + pd.Timedelta(days=7) -> Option B
Quick Check:
Use pd.Timedelta(days=7) for vectorized addition with Series [OK]
Hint: Use pd.Timedelta(days=7) for vectorized addition with Series [OK]
Common Mistakes:
Passing integer without units
Multiplying Timedelta by length of DataFrame
Not using vectorized Timedelta correctly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.