Date arithmetic (Timedelta) in Data Analysis Python - Time & Space Complexity
We want to understand how the time to do date arithmetic changes as we work with more dates.
How does the number of dates affect the time it takes to add or subtract time intervals?
Analyze the time complexity of the following code snippet.
import pandas as pd
dates = pd.date_range(start='2023-01-01', periods=1000)
deltas = pd.to_timedelta(range(1000), unit='D')
new_dates = dates + deltas
This code creates 1000 dates and adds a different number of days to each date using timedelta.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding a timedelta to each date in the list.
- How many times: Once for each date, so 1000 times in this example.
As the number of dates grows, the time to add timedeltas grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 additions |
| 100 | About 100 additions |
| 1000 | About 1000 additions |
Pattern observation: Doubling the number of dates roughly doubles the work done.
Time Complexity: O(n)
This means the time to do date arithmetic grows linearly with the number of dates.
[X] Wrong: "Adding timedeltas to many dates happens instantly no matter how many dates there are."
[OK] Correct: Each date needs its own addition operation, so more dates mean more work and more time.
Understanding how operations scale with data size helps you write efficient code and explain your reasoning clearly.
"What if we added the same timedelta to all dates instead of different ones? How would the time complexity change?"