Skip to content

Commit c79e622

Browse files
Chitrank-DixitTomAugspurger
authored andcommitted
DOC: updated the docstring of pandas.date_range (pandas-dev#20128)
1 parent 521377b commit c79e622

File tree

1 file changed

+93
-27
lines changed

1 file changed

+93
-27
lines changed

pandas/core/indexes/datetimes.py

Lines changed: 93 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,56 +2507,122 @@ def date_range(start=None, end=None, periods=None, freq='D', tz=None,
25072507
"""
25082508
Return a fixed frequency DatetimeIndex.
25092509
2510-
The default frequency is day (calendar).
2510+
Exactly two of the three parameters `start`, `end` and `periods`
2511+
must be specified.
25112512
25122513
Parameters
25132514
----------
2514-
start : string or datetime-like, default None
2515+
start : str or datetime-like, optional
25152516
Left bound for generating dates.
2516-
end : string or datetime-like, default None
2517+
end : str or datetime-like, optional
25172518
Right bound for generating dates.
2518-
periods : integer, default None
2519+
periods : integer, optional
25192520
Number of periods to generate.
2520-
freq : string or DateOffset, default 'D' (calendar daily)
2521-
Frequency strings can have multiples, e.g. '5H'.
2522-
tz : string, default None
2521+
freq : str or DateOffset, default 'D' (calendar daily)
2522+
Frequency strings can have multiples, e.g. '5H'. See
2523+
:ref:`here <timeseries.offset_aliases>` for a list of
2524+
frequency aliases.
2525+
tz : str or tzinfo, optional
25232526
Time zone name for returning localized DatetimeIndex, for example
2524-
Asia/Hong_Kong.
2527+
'Asia/Hong_Kong'. By default, the resulting DatetimeIndex is
2528+
timezone-naive.
25252529
normalize : bool, default False
25262530
Normalize start/end dates to midnight before generating date range.
2527-
name : string, default None
2531+
name : str, default None
25282532
Name of the resulting DatetimeIndex.
2529-
closed : string, default None
2533+
closed : {None, 'left', 'right'}, optional
25302534
Make the interval closed with respect to the given frequency to
2531-
the 'left', 'right', or both sides (None).
2532-
2533-
Notes
2534-
-----
2535-
Of the three parameters: ``start``, ``end``, and ``periods``, exactly two
2536-
must be specified.
2537-
2538-
To learn more about the frequency strings, please see `this link
2539-
<http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases>`__.
2535+
the 'left', 'right', or both sides (None, the default).
2536+
**kwargs
2537+
For compatibility. Has no effect on the result.
25402538
25412539
Returns
25422540
-------
25432541
rng : DatetimeIndex
25442542
25452543
See Also
25462544
--------
2545+
pandas.DatetimeIndex : An immutable container for datetimes.
25472546
pandas.period_range : Return a fixed frequency PeriodIndex.
25482547
pandas.interval_range : Return a fixed frequency IntervalIndex.
25492548
25502549
Examples
25512550
--------
2552-
>>> pd.date_range('2018-10-03', periods=2) # doctest: +NORMALIZE_WHITESPACE
2553-
DatetimeIndex(['2018-10-03', '2018-10-04'], dtype='datetime64[ns]',
2554-
freq='D')
2555-
2556-
>>> pd.date_range(start='2018-01-01', end='20180103')
2557-
... # doctest: +NORMALIZE_WHITESPACE
2558-
DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03'],
2559-
dtype='datetime64[ns]', freq='D')
2551+
**Specifying the values**
2552+
2553+
The next three examples generate the same `DatetimeIndex`, but vary
2554+
the combination of `start`, `end` and `periods`.
2555+
2556+
Specify `start` and `end`, with the default daily frequency.
2557+
2558+
>>> pd.date_range(start='1/1/2018', end='1/08/2018')
2559+
DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04',
2560+
'2018-01-05', '2018-01-06', '2018-01-07', '2018-01-08'],
2561+
dtype='datetime64[ns]', freq='D')
2562+
2563+
Specify `start` and `periods`, the number of periods (days).
2564+
2565+
>>> pd.date_range(start='1/1/2018', periods=8)
2566+
DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04',
2567+
'2018-01-05', '2018-01-06', '2018-01-07', '2018-01-08'],
2568+
dtype='datetime64[ns]', freq='D')
2569+
2570+
Specify `end` and `periods`, the number of periods (days).
2571+
2572+
>>> pd.date_range(end='1/1/2018', periods=8)
2573+
DatetimeIndex(['2017-12-25', '2017-12-26', '2017-12-27', '2017-12-28',
2574+
'2017-12-29', '2017-12-30', '2017-12-31', '2018-01-01'],
2575+
dtype='datetime64[ns]', freq='D')
2576+
2577+
**Other Parameters**
2578+
2579+
Changed the `freq` (frequency) to ``'M'`` (month end frequency).
2580+
2581+
>>> pd.date_range(start='1/1/2018', periods=5, freq='M')
2582+
DatetimeIndex(['2018-01-31', '2018-02-28', '2018-03-31', '2018-04-30',
2583+
'2018-05-31'],
2584+
dtype='datetime64[ns]', freq='M')
2585+
2586+
Multiples are allowed
2587+
2588+
>>> pd.date_range(start='1/1/2018', periods=5, freq='3M')
2589+
DatetimeIndex(['2018-01-31', '2018-04-30', '2018-07-31', '2018-10-31',
2590+
'2019-01-31'],
2591+
dtype='datetime64[ns]', freq='3M')
2592+
2593+
`freq` can also be specified as an Offset object.
2594+
2595+
>>> pd.date_range(start='1/1/2018', periods=5, freq=pd.offsets.MonthEnd(3))
2596+
DatetimeIndex(['2018-01-31', '2018-04-30', '2018-07-31', '2018-10-31',
2597+
'2019-01-31'],
2598+
dtype='datetime64[ns]', freq='3M')
2599+
2600+
Specify `tz` to set the timezone.
2601+
2602+
>>> pd.date_range(start='1/1/2018', periods=5, tz='Asia/Tokyo')
2603+
DatetimeIndex(['2018-01-01 00:00:00+09:00', '2018-01-02 00:00:00+09:00',
2604+
'2018-01-03 00:00:00+09:00', '2018-01-04 00:00:00+09:00',
2605+
'2018-01-05 00:00:00+09:00'],
2606+
dtype='datetime64[ns, Asia/Tokyo]', freq='D')
2607+
2608+
`closed` controls whether to include `start` and `end` that are on the
2609+
boundary. The default includes boundary points on either end.
2610+
2611+
>>> pd.date_range(start='2017-01-01', end='2017-01-04', closed=None)
2612+
DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04'],
2613+
dtype='datetime64[ns]', freq='D')
2614+
2615+
Use ``closed='left'`` to exclude `end` if it falls on the boundary.
2616+
2617+
>>> pd.date_range(start='2017-01-01', end='2017-01-04', closed='left')
2618+
DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03'],
2619+
dtype='datetime64[ns]', freq='D')
2620+
2621+
Use ``closed='right'`` to exclude `start` if it falls on the boundary.
2622+
2623+
>>> pd.date_range(start='2017-01-01', end='2017-01-04', closed='right')
2624+
DatetimeIndex(['2017-01-02', '2017-01-03', '2017-01-04'],
2625+
dtype='datetime64[ns]', freq='D')
25602626
"""
25612627
return DatetimeIndex(start=start, end=end, periods=periods,
25622628
freq=freq, tz=tz, normalize=normalize, name=name,

0 commit comments

Comments
 (0)