Date range creation with date_range in Pandas - Time & Space Complexity
We want to understand how the time to create a range of dates grows as we ask for more dates.
How does the work change when the date range gets longer?
Analyze the time complexity of the following code snippet.
import pandas as pd
# Create a range of dates from start to end with daily frequency
dates = pd.date_range(start='2023-01-01', end='2023-01-10', freq='D')
This code creates a list of dates from January 1 to January 10, one date per day.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Generating each date in the range one by one.
- How many times: Once for each day between start and end dates, inclusive.
As the number of days increases, the work grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 operations (one per day) |
| 100 | 100 operations |
| 1000 | 1000 operations |
Pattern observation: The number of operations grows directly with the number of dates requested.
Time Complexity: O(n)
This means the time to create the date range grows linearly with the number of dates.
[X] Wrong: "Creating a date range is instant no matter how many dates."
[OK] Correct: Each date must be generated and stored, so more dates take more time.
Understanding how data size affects time helps you explain your code choices clearly and confidently.
"What if we changed the frequency from daily to hourly? How would the time complexity change?"