Create a regular time axis
Build a regular daily, hourly, monthly, or custom-frequency timeline for indexing or resampling.
Pandas · Function
pd.date_range creates a fixed-frequency DatetimeIndex from date bounds, a period count, or both. Specify exactly three of start, end, periods, and freq; inclusive controls boundary inclusion.
Call pd.date_range with any three of start, end, periods, and freq. It produces equally spaced timestamps according to the frequency, constrains them to the requested bounds, and returns them as a DatetimeIndex; inclusive controls boundary inclusion, while tz, normalize, name, and unit refine the result.
pandas.date_range(start=None, end=None, periods=None, freq=None, tz=None, normalize=False, name=None, inclusive='both', *, unit=None, **kwargs)| Name | Type | Description |
|---|---|---|
start | str or datetime-like, optional | Left bound for generating dates. |
end | str or datetime-like, optional | Right bound for generating dates. |
periods | int, optional | Number of periods to generate. |
freq | str, Timedelta, datetime.timedelta, or DateOffset, default 'D' | Frequency for the generated timestamps; defaults to calendar daily frequency. |
tz | str, tzinfo, or None, optional | Timezone for the returned localized DatetimeIndex. |
normalize | bool, default False | Normalize start and end dates to midnight before generating. |
name | str, default None | Name assigned to the resulting DatetimeIndex. |
inclusive | {'both', 'neither', 'left', 'right'}, default 'both' | Which endpoints to include: 'both', 'neither', 'left', or 'right'. |
unit | str, default None | Resolution of the resulting DatetimeIndex. |
**kwargs | dict | Additional keyword arguments accepted by the function. |
DatetimeIndex: A DatetimeIndex object of the generated dates.
tz to produce a localized DatetimeIndex for a named timezone.normalize=True when the input boundaries should be converted to midnight first.name to assign a name to the returned index.start, end, periods, and freq; omitting too many or supplying all four is invalid.inclusive deliberately when endpoints should be excluded or only one boundary should remain.import pandas as pd
quarter_ends = pd.date_range(
start="2024-01-01",
end="2024-12-31",
freq="QE",
inclusive="both",
)
print(quarter_ends)Creates a DatetimeIndex containing quarter-end timestamps within the supplied bounds.
Build a regular daily, hourly, monthly, or custom-frequency timeline for indexing or resampling.
Generate a known number of equally spaced timestamps from a starting point.
Create localized timestamps for calculations or display in a particular timezone.
pd.date_range return and how is spacing determined?It returns a DatetimeIndex containing equally spaced time points. The spacing comes from freq, and the result is restricted to the requested start and end boundaries.
Follow-up: How would you generate a fixed number of timestamps instead? Supply periods and freq, using one boundary if needed, while still specifying exactly three of the four core arguments.
The function requires exactly three of start, end, periods, and freq. periods sets the number of generated dates, while start and end define bounds and freq defines spacing.
Follow-up: What happens if both bounds and periods are supplied? Add freq as the third required core argument; otherwise the call does not satisfy the documented combination rule.
inclusive accepts both, neither, left, or right, with both as the default. It determines whether the start and end boundaries are included in the result.
Follow-up: How can the result be timezone-aware? Pass a timezone name or tzinfo through tz, which is documented as controlling localization of the returned DatetimeIndex.
pandas.bdate_rangepandas.period_rangepandas.timedelta_rangepandas.DatetimeIndex