Skip to content

Commit 7ca9841

Browse files
committed
DOC: Clarify note on date_range/timedelta_range arguments
1 parent 728be93 commit 7ca9841

File tree

1 file changed

+35
-155
lines changed

1 file changed

+35
-155
lines changed

pandas/core/indexes/datetimes.py

Lines changed: 35 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -836,176 +836,56 @@ def date_range(
836836
"""
837837
Return a fixed frequency DatetimeIndex.
838838
839-
Returns the range of equally spaced time points (where the difference between any
840-
two adjacent points is specified by the given frequency) such that they fall in the
841-
range `[start, end]` , where the first one and the last one are, resp., the first
842-
and last time points in that range that fall on the boundary of ``freq`` (if given
843-
as a frequency string) or that are valid for ``freq`` (if given as a
844-
:class:`pandas.tseries.offsets.DateOffset`). If ``freq`` is positive, the points
845-
satisfy `start <[=] x <[=] end`, and if ``freq`` is negative, the points satisfy
846-
`end <[=] x <[=] start`. (If exactly one of ``start``, ``end``, or ``freq`` is *not*
847-
specified, this missing parameter can be computed given ``periods``, the number of
848-
timesteps in the range. See the note below.)
839+
:param start: Left bound for generating dates. Accepts string or datetime-like object. Optional.
840+
:type start: str or datetime-like, optional
849841
850-
Parameters
851-
----------
852-
start : str or datetime-like, optional
853-
Left bound for generating dates.
854-
end : str or datetime-like, optional
855-
Right bound for generating dates.
856-
periods : int, optional
857-
Number of periods to generate.
858-
freq : str, Timedelta, datetime.timedelta, or DateOffset, default 'D'
859-
Frequency strings can have multiples, e.g. '5h'. See
860-
:ref:`here <timeseries.offset_aliases>` for a list of
861-
frequency aliases.
862-
tz : str or tzinfo, optional
863-
Time zone name for returning localized DatetimeIndex, for example
864-
'Asia/Hong_Kong'. By default, the resulting DatetimeIndex is
865-
timezone-naive unless timezone-aware datetime-likes are passed.
866-
normalize : bool, default False
867-
Normalize start/end dates to midnight before generating date range.
868-
name : str, default None
869-
Name of the resulting DatetimeIndex.
870-
inclusive : {"both", "neither", "left", "right"}, default "both"
871-
Include boundaries; Whether to set each bound as closed or open.
842+
:param end: Right bound for generating dates. Accepts string or datetime-like object. Optional.
843+
:type end: str or datetime-like, optional
872844
873-
.. versionadded:: 1.4.0
874-
unit : str, default None
875-
Specify the desired resolution of the result.
845+
:param periods: Number of periods to generate. Mutually exclusive with one of `start` or `end`. Optional.
846+
:type periods: int, optional
876847
877-
.. versionadded:: 2.0.0
878-
**kwargs
879-
For compatibility. Has no effect on the result.
848+
:param freq: Frequency string or offset. Determines the spacing between dates. Defaults to 'D' (day) if not specified and enough information is provided. Accepts string (e.g., '5h'), Timedelta, datetime.timedelta, or DateOffset.
849+
:type freq: str, Timedelta, datetime.timedelta, or DateOffset, optional
880850
881-
Returns
882-
-------
883-
DatetimeIndex
884-
A DatetimeIndex object of the generated dates.
851+
:param tz: Time zone name or tzinfo to localize the DatetimeIndex. If not specified, the result is timezone-naive unless timezone-aware inputs are provided.
852+
:type tz: str or tzinfo, optional
885853
886-
See Also
887-
--------
888-
DatetimeIndex : An immutable container for datetimes.
889-
timedelta_range : Return a fixed frequency TimedeltaIndex.
890-
period_range : Return a fixed frequency PeriodIndex.
891-
interval_range : Return a fixed frequency IntervalIndex.
854+
:param normalize: Whether to normalize start/end dates to midnight before generating the date range.
855+
:type normalize: bool, default False
892856
893-
Notes
894-
-----
895-
Of the four parameters ``start``, ``end``, ``periods``, and ``freq``,
896-
exactly three must be specified. If ``freq`` is omitted, the resulting
897-
``DatetimeIndex`` will have ``periods`` linearly spaced elements between
898-
``start`` and ``end`` (closed on both sides).
857+
:param name: Name to assign to the resulting DatetimeIndex.
858+
:type name: str, optional
899859
900-
To learn more about the frequency strings, please see
901-
:ref:`this link<timeseries.offset_aliases>`.
860+
:param inclusive: Whether to include boundaries. One of {"both", "neither", "left", "right"}. Default is "both".
861+
:type inclusive: {"both", "neither", "left", "right"}, default "both"
902862
903-
Examples
904-
--------
905-
**Specifying the values**
906-
907-
The next four examples generate the same `DatetimeIndex`, but vary
908-
the combination of `start`, `end` and `periods`.
909-
910-
Specify `start` and `end`, with the default daily frequency.
911-
912-
>>> pd.date_range(start="1/1/2018", end="1/08/2018")
913-
DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04',
914-
'2018-01-05', '2018-01-06', '2018-01-07', '2018-01-08'],
915-
dtype='datetime64[ns]', freq='D')
916-
917-
Specify timezone-aware `start` and `end`, with the default daily frequency.
918-
919-
>>> pd.date_range(
920-
... start=pd.to_datetime("1/1/2018").tz_localize("Europe/Berlin"),
921-
... end=pd.to_datetime("1/08/2018").tz_localize("Europe/Berlin"),
922-
... )
923-
DatetimeIndex(['2018-01-01 00:00:00+01:00', '2018-01-02 00:00:00+01:00',
924-
'2018-01-03 00:00:00+01:00', '2018-01-04 00:00:00+01:00',
925-
'2018-01-05 00:00:00+01:00', '2018-01-06 00:00:00+01:00',
926-
'2018-01-07 00:00:00+01:00', '2018-01-08 00:00:00+01:00'],
927-
dtype='datetime64[ns, Europe/Berlin]', freq='D')
928-
929-
Specify `start` and `periods`, the number of periods (days).
930-
931-
>>> pd.date_range(start="1/1/2018", periods=8)
932-
DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04',
933-
'2018-01-05', '2018-01-06', '2018-01-07', '2018-01-08'],
934-
dtype='datetime64[ns]', freq='D')
935-
936-
Specify `end` and `periods`, the number of periods (days).
937-
938-
>>> pd.date_range(end="1/1/2018", periods=8)
939-
DatetimeIndex(['2017-12-25', '2017-12-26', '2017-12-27', '2017-12-28',
940-
'2017-12-29', '2017-12-30', '2017-12-31', '2018-01-01'],
941-
dtype='datetime64[ns]', freq='D')
942-
943-
Specify `start`, `end`, and `periods`; the frequency is generated
944-
automatically (linearly spaced).
863+
:param unit: Specify the desired resolution of the result (e.g., 's' for seconds).
864+
:type unit: str, optional
945865
946-
>>> pd.date_range(start="2018-04-24", end="2018-04-27", periods=3)
947-
DatetimeIndex(['2018-04-24 00:00:00', '2018-04-25 12:00:00',
948-
'2018-04-27 00:00:00'],
949-
dtype='datetime64[ns]', freq=None)
866+
:param kwargs: Additional keyword arguments for compatibility. Has no effect on the result.
950867
951-
**Other Parameters**
868+
:return: DatetimeIndex of the generated dates.
869+
:rtype: DatetimeIndex
952870
953-
Changed the `freq` (frequency) to ``'ME'`` (month end frequency).
871+
:raises ValueError: If less than three of the four parameters (`start`, `end`, `periods`, `freq`) are specified.
954872
955-
>>> pd.date_range(start="1/1/2018", periods=5, freq="ME")
956-
DatetimeIndex(['2018-01-31', '2018-02-28', '2018-03-31', '2018-04-30',
957-
'2018-05-31'],
958-
dtype='datetime64[ns]', freq='ME')
873+
.. note::
874+
Of the four parameters (`start`, `end`, `periods`, `freq`), exactly three must be specified. If `freq` is omitted, the resulting DatetimeIndex will have `periods` linearly spaced elements between `start` and `end` (inclusive or exclusive depending on `inclusive`).
959875
960-
Multiples are allowed
876+
.. seealso::
877+
:class:`pandas.DatetimeIndex` : Immutable container for datetimes.
878+
:func:`pandas.timedelta_range` : Return a fixed frequency TimedeltaIndex.
879+
:func:`pandas.period_range` : Return a fixed frequency PeriodIndex.
880+
:func:`pandas.interval_range` : Return a fixed frequency IntervalIndex.
961881
962-
>>> pd.date_range(start="1/1/2018", periods=5, freq="3ME")
963-
DatetimeIndex(['2018-01-31', '2018-04-30', '2018-07-31', '2018-10-31',
964-
'2019-01-31'],
965-
dtype='datetime64[ns]', freq='3ME')
966-
967-
`freq` can also be specified as an Offset object.
968-
969-
>>> pd.date_range(start="1/1/2018", periods=5, freq=pd.offsets.MonthEnd(3))
970-
DatetimeIndex(['2018-01-31', '2018-04-30', '2018-07-31', '2018-10-31',
971-
'2019-01-31'],
972-
dtype='datetime64[ns]', freq='3ME')
973-
974-
Specify `tz` to set the timezone.
975-
976-
>>> pd.date_range(start="1/1/2018", periods=5, tz="Asia/Tokyo")
977-
DatetimeIndex(['2018-01-01 00:00:00+09:00', '2018-01-02 00:00:00+09:00',
978-
'2018-01-03 00:00:00+09:00', '2018-01-04 00:00:00+09:00',
979-
'2018-01-05 00:00:00+09:00'],
980-
dtype='datetime64[ns, Asia/Tokyo]', freq='D')
981-
982-
`inclusive` controls whether to include `start` and `end` that are on the
983-
boundary. The default, "both", includes boundary points on either end.
984-
985-
>>> pd.date_range(start="2017-01-01", end="2017-01-04", inclusive="both")
986-
DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04'],
987-
dtype='datetime64[ns]', freq='D')
988-
989-
Use ``inclusive='left'`` to exclude `end` if it falls on the boundary.
990-
991-
>>> pd.date_range(start="2017-01-01", end="2017-01-04", inclusive="left")
992-
DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03'],
993-
dtype='datetime64[ns]', freq='D')
994-
995-
Use ``inclusive='right'`` to exclude `start` if it falls on the boundary, and
996-
similarly ``inclusive='neither'`` will exclude both `start` and `end`.
997-
998-
>>> pd.date_range(start="2017-01-01", end="2017-01-04", inclusive="right")
999-
DatetimeIndex(['2017-01-02', '2017-01-03', '2017-01-04'],
1000-
dtype='datetime64[ns]', freq='D')
1001-
1002-
**Specify a unit**
882+
Examples
883+
--------
884+
>>> pd.date_range(start="2020-01-01", end="2020-01-05")
885+
DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04', '2020-01-05'], dtype='datetime64[ns]', freq='D')
1003886
1004-
>>> pd.date_range(start="2017-01-01", periods=10, freq="100YS", unit="s")
1005-
DatetimeIndex(['2017-01-01', '2117-01-01', '2217-01-01', '2317-01-01',
1006-
'2417-01-01', '2517-01-01', '2617-01-01', '2717-01-01',
1007-
'2817-01-01', '2917-01-01'],
1008-
dtype='datetime64[s]', freq='100YS-JAN')
887+
>>> pd.date_range(start="2020-01-01", periods=3, freq="2D")
888+
DatetimeIndex(['2020-01-01', '2020-01-03', '2020-01-05'], dtype='datetime64[ns]', freq='2D')
1009889
"""
1010890
if freq is None and com.any_none(periods, start, end):
1011891
freq = "D"

0 commit comments

Comments
 (0)