Pandas | interval_range method
Start your free 7-days trial now!
Pandas interval_range(~)
method returns an IntervalIndex
with fixed frequency.
If you are dealing with time-related data, then opt to use period_range(~)
instead.
Parameters
1. start
link | numeric
or datetime-like
| optional
The lower bound of the range. By default, start=None
.
2. end
link | numeric
or datetime-like
| optional
The upper bound of the range. By default, end=None
.
3. periods
link | int
| optional
The desired number of dates. By default, periods=True
.
Out of the above three parameters, exactly two must be specified - no less, no more.
4. freq
link | string
or DateOffset
| optional
The length of interval. By default, if start
is numeric, freq=1
, otherwise freq="D"
(a length of one day).
5. name
link | string
| optional
The name assigned to the resulting IntervalIndex
. By default, name=None
.
6. closed
link | string
| optional
Whether or not to make the bounds inclusive/exclusive:
Value | Description |
---|---|
|
|
|
|
| Both endpoints are inclusive. |
| Both endpoints are exclusive. |
By default, closed="right"
.
Return Value
An IntervalIndex
.
Examples
Basic usage
Numeric
To create an interval range using simple numerics:
pd.interval_range(start=5, end=8)
IntervalIndex([(5, 6], (6, 7], (7, 8]], closed='right', dtype='interval[int64]')
Here, the returned IntervalIndex
holds 3 intervals. Note that (5,6]
just means 5 < a <=6
.
Dates
To create a sequence of date intervals from 2020-12-25
(inclusive) to 2020-12-28
(exclusive):
The default step-size (the frequency) is a day, so this is why we see freq="D"
in the output.
Specifying period
To create a sequence of 3 numeric intervals:
pd.interval_range(start=5, periods=3)
IntervalIndex([(5, 6], (6, 7], (7, 8]], closed='right', dtype='interval[int64]')
To create a sequence of 3 date intervals from 2020-12-25
(inclusive):
IntervalIndex([(2020-12-25, 2020-12-26], (2020-12-26, 2020-12-27], (2020-12-27, 2020-12-28]], closed='right', dtype='interval[datetime64[ns]]')
Specifying frequency
For numeric intervals, by default, freq=1
, which means that the interval width is 1
:
pd.interval_range(start=5, periods=3) # freq=1
IntervalIndex([(5, 6], (6, 7], (7, 8]], closed='right', dtype='interval[int64]')
To set the interval width to 2:
pd.interval_range(start=5, periods=3, freq=2)
IntervalIndex([(5, 7], (7, 9], (9, 11]], closed='right', dtype='interval[int64]')
For time series, by default, freq="D"
, which means that the interval width is a single day:
IntervalIndex([(2020-12-25, 2020-12-26], (2020-12-26, 2020-12-27], (2020-12-27, 2020-12-28]], closed='right', dtype='interval[datetime64[ns]]')
To set the interval width to 2 days:
IntervalIndex([(2020-12-25, 2020-12-27], (2020-12-27, 2020-12-29], (2020-12-29, 2020-12-31]], closed='right', dtype='interval[datetime64[ns]]')
Specifying name
To give a name to the resulting IntervalIndex
:
IntervalIndex([(2020-12-25, 2020-12-26], (2020-12-26, 2020-12-27], (2020-12-27, 2020-12-28]], closed='right', name='My Dates', dtype='interval[datetime64[ns]]')
Notice how we have name="My Dates"
encoded into the IntervalIndex
.
Specifying closed
By default, the lower bound is exclusive and the upper bound is inclusive (closed="right"
):
IntervalIndex([(2020-12-25, 2020-12-26], (2020-12-26, 2020-12-27], (2020-12-27, 2020-12-28]], closed='right', dtype='interval[datetime64[ns]]')
To make the lower bound inclusive and the upper bound exclusive, set closed="left"
:
IntervalIndex([[2020-12-25, 2020-12-26), [2020-12-26, 2020-12-27), [2020-12-27, 2020-12-28)], closed='left', dtype='interval[datetime64[ns]]')
To make both bounds inclusive, set closed="both"
:
IntervalIndex([[2020-12-25, 2020-12-26], [2020-12-26, 2020-12-27], [2020-12-27, 2020-12-28]], closed='both', dtype='interval[datetime64[ns]]')
To make both bounds exclusive, set closed="neither"
:
IntervalIndex([(2020-12-25, 2020-12-26), (2020-12-26, 2020-12-27), (2020-12-27, 2020-12-28)], closed='neither', dtype='interval[datetime64[ns]]')
Using IntervalIndex to initialise a DataFrame
IntervalIndex
can be used as the index of a DataFrame:
idx = pd.interval_range(start=5, end=7)
A B(5, 6] 3 5(6, 7] 4 6