Date arithmetic with timedelta in Pandas - Time & Space Complexity
We want to understand how the time it takes to do date calculations changes as we work with more dates.
Specifically, how does adding or subtracting time from many dates affect the work done?
Analyze the time complexity of the following code snippet.
import pandas as pd
from datetime import timedelta
dates = pd.date_range('2023-01-01', periods=1000)
delta = timedelta(days=5)
new_dates = dates + delta
This code creates 1000 dates and adds 5 days to each date.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding a fixed time difference to each date in the list.
- How many times: Once for each date, so 1000 times in this example.
When we add a time difference to each date, the work grows as we have more dates.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of dates. Double the dates, double the work.
Time Complexity: O(n)
This means the time to add a timedelta grows in a straight line with the number of dates.
[X] Wrong: "Adding a timedelta to a list of dates happens instantly regardless of list size."
[OK] Correct: Each date must be updated one by one, so more dates mean more work and more time.
Understanding how operations scale with data size helps you write efficient code and explain your choices clearly.
"What if we added a different timedelta to each date instead of the same one? How would the time complexity change?"