Skip to content

Commit 3e91eb9

Browse files
jschendeljorisvandenbossche
authored andcommitted
DOC: Add linspace range behavior to the timeseries/timedeltas/interval docs (#21114)
* DOC: Add linspace range behavior to the timeseries/timedeltas/interval docs (cherry picked from commit 90c2237)
1 parent 58eaecb commit 3e91eb9

File tree

3 files changed

+99
-4
lines changed

3 files changed

+99
-4
lines changed

doc/source/advanced.rst

+49
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,55 @@ bins, with ``NaN`` representing a missing value similar to other dtypes.
924924
925925
pd.cut([0, 3, 5, 1], bins=c.categories)
926926
927+
928+
Generating Ranges of Intervals
929+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
930+
931+
If we need intervals on a regular frequency, we can use the :func:`interval_range` function
932+
to create an ``IntervalIndex`` using various combinations of ``start``, ``end``, and ``periods``.
933+
The default frequency for ``interval_range`` is a 1 for numeric intervals, and calendar day for
934+
datetime-like intervals:
935+
936+
.. ipython:: python
937+
938+
pd.interval_range(start=0, end=5)
939+
940+
pd.interval_range(start=pd.Timestamp('2017-01-01'), periods=4)
941+
942+
pd.interval_range(end=pd.Timedelta('3 days'), periods=3)
943+
944+
The ``freq`` parameter can used to specify non-default frequencies, and can utilize a variety
945+
of :ref:`frequency aliases <timeseries.offset_aliases>` with datetime-like intervals:
946+
947+
.. ipython:: python
948+
949+
pd.interval_range(start=0, periods=5, freq=1.5)
950+
951+
pd.interval_range(start=pd.Timestamp('2017-01-01'), periods=4, freq='W')
952+
953+
pd.interval_range(start=pd.Timedelta('0 days'), periods=3, freq='9H')
954+
955+
Additionally, the ``closed`` parameter can be used to specify which side(s) the intervals
956+
are closed on. Intervals are closed on the right side by default.
957+
958+
.. ipython:: python
959+
960+
pd.interval_range(start=0, end=4, closed='both')
961+
962+
pd.interval_range(start=0, end=4, closed='neither')
963+
964+
.. versionadded:: 0.23.0
965+
966+
Specifying ``start``, ``end``, and ``periods`` will generate a range of evenly spaced
967+
intervals from ``start`` to ``end`` inclusively, with ``periods`` number of elements
968+
in the resulting ``IntervalIndex``:
969+
970+
.. ipython:: python
971+
972+
pd.interval_range(start=0, end=6, periods=4)
973+
974+
pd.interval_range(pd.Timestamp('2018-01-01'), pd.Timestamp('2018-02-28'), periods=3)
975+
927976
Miscellaneous indexing FAQ
928977
--------------------------
929978

doc/source/timedeltas.rst

+38-4
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,8 @@ You can convert a ``Timedelta`` to an `ISO 8601 Duration`_ string with the
352352
TimedeltaIndex
353353
--------------
354354

355-
To generate an index with time delta, you can use either the ``TimedeltaIndex`` or
356-
the ``timedelta_range`` constructor.
355+
To generate an index with time delta, you can use either the :class:`TimedeltaIndex` or
356+
the :func:`timedelta_range` constructor.
357357

358358
Using ``TimedeltaIndex`` you can pass string-like, ``Timedelta``, ``timedelta``,
359359
or ``np.timedelta64`` objects. Passing ``np.nan/pd.NaT/nat`` will represent missing values.
@@ -363,13 +363,47 @@ or ``np.timedelta64`` objects. Passing ``np.nan/pd.NaT/nat`` will represent miss
363363
pd.TimedeltaIndex(['1 days', '1 days, 00:00:05',
364364
np.timedelta64(2,'D'), datetime.timedelta(days=2,seconds=2)])
365365
366-
Similarly to ``date_range``, you can construct regular ranges of a ``TimedeltaIndex``:
366+
Generating Ranges of Time Deltas
367+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
368+
369+
Similar to :func:`date_range`, you can construct regular ranges of a ``TimedeltaIndex``
370+
using :func:`timedelta_range`. The default frequency for ``timedelta_range`` is
371+
calendar day:
372+
373+
.. ipython:: python
374+
375+
pd.timedelta_range(start='1 days', periods=5)
376+
377+
Various combinations of ``start``, ``end``, and ``periods`` can be used with
378+
``timedelta_range``:
379+
380+
.. ipython:: python
381+
382+
pd.timedelta_range(start='1 days', end='5 days')
383+
384+
pd.timedelta_range(end='10 days', periods=4)
385+
386+
The ``freq`` parameter can passed a variety of :ref:`frequency aliases <timeseries.offset_aliases>`:
367387

368388
.. ipython:: python
369389
370-
pd.timedelta_range(start='1 days', periods=5, freq='D')
371390
pd.timedelta_range(start='1 days', end='2 days', freq='30T')
372391
392+
pd.timedelta_range(start='1 days', periods=5, freq='2D5H')
393+
394+
395+
.. versionadded:: 0.23.0
396+
397+
Specifying ``start``, ``end``, and ``periods`` will generate a range of evenly spaced
398+
timedeltas from ``start`` to ``end`` inclusively, with ``periods`` number of elements
399+
in the resulting ``TimedeltaIndex``:
400+
401+
.. ipython:: python
402+
403+
pd.timedelta_range('0 days', '4 days', periods=5)
404+
405+
pd.timedelta_range('0 days', '4 days', periods=10)
406+
373407
Using the TimedeltaIndex
374408
~~~~~~~~~~~~~~~~~~~~~~~~
375409

doc/source/timeseries.rst

+12
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,18 @@ of those specified will not be generated:
393393
394394
pd.bdate_range(start=start, periods=20)
395395
396+
.. versionadded:: 0.23.0
397+
398+
Specifying ``start``, ``end``, and ``periods`` will generate a range of evenly spaced
399+
dates from ``start`` to ``end`` inclusively, with ``periods`` number of elements in the
400+
resulting ``DatetimeIndex``:
401+
402+
.. ipython:: python
403+
404+
pd.date_range('2018-01-01', '2018-01-05', periods=5)
405+
406+
pd.date_range('2018-01-01', '2018-01-05', periods=10)
407+
396408
.. _timeseries.custom-freq-ranges:
397409

398410
Custom Frequency Ranges

0 commit comments

Comments
 (0)