0
0
Pandasdata~15 mins

Date arithmetic with timedelta in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Date Arithmetic with timedelta in pandas
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(shipments) to display the DataFrame.