← Back to Pandas Course | Chapter 10: Time Series | Lesson 6 of 7

pd.date_range()

date_range creates a regular sequence of dates for building indexes and test data.

In this page:

  1. pd.date_range()
Syntax
python
pd.date_range(start="start_date", periods=n, freq="D")
pd.date_range(start="start_date", end="end_date", freq="W")

pd.date_range()

pd.date_range(start, end or periods, freq) generates timestamps at a chosen frequency such as "D" for days or "W" for weeks. Use either end or periods, not both. bdate_range skips weekends.

Note: pd.bdate_range gives business days only.

Example: pd.date_range()

python
import pandas as pd

print(pd.date_range("2024-01-01", "2024-01-05"))
print(pd.date_range("2024-01-01", periods=3, freq="W"))
print(pd.bdate_range("2024-01-05", periods=3))

# Output:
# DatetimeIndex(['2024-01-01', '2024-01-02', '2024-01-03', '2024-01-04',
#                '2024-01-05'],
#               dtype='datetime64[ns]', freq='D')
# DatetimeIndex(['2024-01-07', '2024-01-14', '2024-01-21'], dtype='datetime64[ns]', freq='W-SUN')
# DatetimeIndex(['2024-01-05', '2024-01-08', '2024-01-09'], dtype='datetime64[ns]', freq='B')
Related Topics
Common Mistakes
  1. Giving both end and periods
  2. Misjudging inclusive ends
  3. Using an unknown frequency alias
Chapter Summary
  • date_range builds regular timestamps
  • freq sets the step
  • Use end or periods
  • bdate_range skips weekends
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.