0
0
Pandasdata~5 mins

Date arithmetic with timedelta in Pandas - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Date arithmetic with timedelta
O(n)
Understanding Time 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?

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

When we add a time difference to each date, the work grows as we have more dates.

Input Size (n)Approx. Operations
1010 additions
100100 additions
10001000 additions

Pattern observation: The work grows directly with the number of dates. Double the dates, double the work.

Final Time Complexity

Time Complexity: O(n)

This means the time to add a timedelta grows in a straight line with the number of dates.

Common Mistake

[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.

Interview Connect

Understanding how operations scale with data size helps you write efficient code and explain your choices clearly.

Self-Check

"What if we added a different timedelta to each date instead of the same one? How would the time complexity change?"