0
0
Data Analysis Pythondata~30 mins

Date arithmetic (Timedelta) in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Date Arithmetic with Timedelta in Python
📖 Scenario: Imagine you work in a library. You have a list of books with their borrow dates. You want to find out when each book should be returned by adding a fixed borrowing period.
🎯 Goal: You will create a dictionary of books with their borrow dates, set a borrowing period, calculate the return dates by adding the borrowing period to each borrow date, and finally display the return dates.
📋 What You'll Learn
Create a dictionary with book titles as keys and borrow dates as datetime objects as values
Create a variable for the borrowing period using timedelta
Calculate return dates by adding the borrowing period to each borrow date using a dictionary comprehension
Print the dictionary of return dates
💡 Why This Matters
🌍 Real World
Libraries, rental services, and any system that tracks borrowed items use date arithmetic to calculate due dates.
💼 Career
Understanding date arithmetic is important for data analysts and scientists working with time series data, scheduling, and event tracking.
Progress0 / 4 steps
1
Create the dictionary of borrow dates
Create a dictionary called borrow_dates with these exact entries: 'Book A': datetime(2024, 6, 1), 'Book B': datetime(2024, 6, 3), 'Book C': datetime(2024, 6, 5). Import datetime from the datetime module.
Data Analysis Python
Hint

Use datetime(2024, 6, 1) to create the date for June 1, 2024.

2
Set the borrowing period
Import timedelta from the datetime module and create a variable called borrow_period that represents 14 days.
Data Analysis Python
Hint

Use timedelta(days=14) to represent 14 days.

3
Calculate return dates using dictionary comprehension
Create a new dictionary called return_dates using a dictionary comprehension. For each book and borrow_date in borrow_dates.items(), add borrow_period to borrow_date.
Data Analysis Python
Hint

Use {book: borrow_date + borrow_period for book, borrow_date in borrow_dates.items()} to create the new dictionary.

4
Print the return dates
Print the return_dates dictionary to see the calculated return dates.
Data Analysis Python
Hint

Use print(return_dates) to display the dictionary.