🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

pd.date_range()

AI & DATA SCIENCE // pd-date-range

pd.date_range() generates a sequence of evenly-spaced datetime values, given a start, an end (or a number of periods), and a frequency.

Syntax

pd.date_range(start=None, end=None, periods=None, freq='D')

Deep Dive Course

date_range() needs any two of start, end, and periods, plus a frequency, to fully determine the sequence — given a start and an end, it fills in every date at the given interval between them; given a start and a number of periods, it generates exactly that many dates starting there. The freq parameter accepts a wide range of frequency codes, 'D' for daily, the default, 'W' for weekly, 'M' for month-end, 'H' for hourly, and many more, often combined with a number for a larger step, making it a flexible way to build the backbone index for regularly-spaced time series data.

1Understanding pd.date_range()

date_range() needs any two of start, end, and periods, plus a frequency, to fully determine the sequence — given a start and an end, it fills in every date at the given interval between them; given a start and a number of periods, it generates exactly that many dates starting there. The freq parameter accepts a wide range of frequency codes, 'D' for daily, the default, 'W' for weekly, 'M' for month-end, 'H' for hourly, and many more, often combined with a number for a larger step, making it a flexible way to build the backbone index for regularly-spaced time series data.

💡

Use date_range() to build the complete, regularly-spaced index a time series should have, then reindex your actual, possibly gappy, data onto it — this is a standard technique for making missing dates in a time series explicit as NaN rows, rather than silently absent.

editor.html
import pandas as pd

dates = pd.date_range(start="2026-01-01", periods=5, freq="D")
print(dates)
localhost:3000

2Practical Example

Here is a real-world application of pd.date_range() showing how it is used in production Pandas code.

editor.html
import pandas as pd

dates = pd.date_range(start="2026-01-01", end="2026-03-01", freq="MS")
print(dates)
localhost:3000

3Best Practices

Follow these guidelines when working with pd.date_range():

1. Choose the freq code deliberately to match your data's actual cadence, daily, weekly, monthly, etc., rather than defaulting to 'D' without thinking about it

2. Use date_range() to build a complete reference index, then reindex actual data onto it, to make gaps in a time series explicit rather than silently missing

3. Combine start/periods, rather than start/end, when you know exactly how many data points you need, regardless of what date that lands on

⚠️

Tip: Use date_range() to build the complete, regularly-spaced index a time series should have, then reindex your actual, possibly gappy, data onto it — this is a standard technique for making missing dates in a time series explicit as NaN rows, rather than silently absent.

editor.html
import pandas as pd

dates = pd.date_range(start="2026-01-01", periods=5, freq="D")
print(dates)
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

dates = pd.date_range(start="2026-01-01", periods=5, freq="D")
print(dates)
Example 02Advanced Example
import pandas as pd

dates = pd.date_range(start="2026-01-01", end="2026-03-01", freq="MS")
print(dates)

Best Practices

  • Choose the freq code deliberately to match your data's actual cadence, daily, weekly, monthly, etc., rather than defaulting to 'D' without thinking about it
  • Use date_range() to build a complete reference index, then reindex actual data onto it, to make gaps in a time series explicit rather than silently missing
  • Combine start/periods, rather than start/end, when you know exactly how many data points you need, regardless of what date that lands on

Interview Question

If pd.date_range() is given a start and an end date, but no explicit number of periods, how does it decide how many dates to generate?

Hint: Think about what determines the count once you already know the frequency and the boundaries.

Once you specify a start, an end, and a frequency, the number of dates is fully determined — date_range() simply starts at the start date and keeps stepping forward by the given frequency interval until it reaches or would exceed the end date, including the end date itself if it falls exactly on the frequency's stepping pattern. There's no separate periods count needed in this case, since start, end, and freq together already pin down exactly how many evenly-spaced points fit in that range.

Exercises

MediumPractice using pd.date_range() in a real scenario.
View Solution
import pandas as pd

dates = pd.date_range(start="2026-01-01", periods=5, freq="D")
print(dates)

Frequently Asked Questions

If pd.date_range() is given a start and an end date, but no explicit number of periods, how does it decide how many dates to generate?

Once you specify a start, an end, and a frequency, the number of dates is fully determined — date_range() simply starts at the start date and keeps stepping forward by the given frequency interval until it reaches or would exceed the end date, including the end date itself if it falls exactly on the frequency's stepping pattern. There's no separate periods count needed in this case, since start, end, and freq together already pin down exactly how many evenly-spaced points fit in that range.

Related Functions

pd-to-datetimedf-resampledf-shift