- API reference
- General functions
- pandas.inter...
pandas.interval_range#
- pandas.interval_range(start=None,end=None,periods=None,freq=None,name=None,closed='right')[source]#
Return a fixed frequency IntervalIndex.
- Parameters:
- startnumeric or datetime-like, default None
Left bound for generating intervals.
- endnumeric or datetime-like, default None
Right bound for generating intervals.
- periodsint, default None
Number of periods to generate.
- freqnumeric, str, Timedelta, datetime.timedelta, or DateOffset, default None
The length of each interval. Must be consistent with the type of startand end, e.g. 2 for numeric, or ‘5H’ for datetime-like. Default is 1for numeric and ‘D’ for datetime-like.
- namestr, default None
Name of the resulting IntervalIndex.
- closed{‘left’, ‘right’, ‘both’, ‘neither’}, default ‘right’
Whether the intervals are closed on the left-side, right-side, bothor neither.
- Returns:
- IntervalIndex
See also
IntervalIndexAn Index of intervals that are all closed on the same side.
Notes
Of the four parameters
start,end,periods, andfreq,exactly three must be specified. Iffreqis omitted, the resultingIntervalIndexwill haveperiodslinearly spaced elements betweenstartandend, inclusively.To learn more about datetime-like frequency strings, please seethis link.
Examples
Numeric
startandendis supported.>>>pd.interval_range(start=0,end=5)IntervalIndex([(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]], dtype='interval[int64, right]')
Additionally, datetime-like input is also supported.
>>>pd.interval_range(start=pd.Timestamp('2017-01-01'),...end=pd.Timestamp('2017-01-04'))IntervalIndex([(2017-01-01 00:00:00, 2017-01-02 00:00:00], (2017-01-02 00:00:00, 2017-01-03 00:00:00], (2017-01-03 00:00:00, 2017-01-04 00:00:00]], dtype='interval[datetime64[ns], right]')
The
freqparameter specifies the frequency between the left and right.endpoints of the individual intervals within theIntervalIndex. Fornumericstartandend, the frequency must also be numeric.>>>pd.interval_range(start=0,periods=4,freq=1.5)IntervalIndex([(0.0, 1.5], (1.5, 3.0], (3.0, 4.5], (4.5, 6.0]], dtype='interval[float64, right]')
Similarly, for datetime-like
startandend, the frequency must beconvertible to a DateOffset.>>>pd.interval_range(start=pd.Timestamp('2017-01-01'),...periods=3,freq='MS')IntervalIndex([(2017-01-01 00:00:00, 2017-02-01 00:00:00], (2017-02-01 00:00:00, 2017-03-01 00:00:00], (2017-03-01 00:00:00, 2017-04-01 00:00:00]], dtype='interval[datetime64[ns], right]')
Specify
start,end, andperiods; the frequency is generatedautomatically (linearly spaced).>>>pd.interval_range(start=0,end=6,periods=4)IntervalIndex([(0.0, 1.5], (1.5, 3.0], (3.0, 4.5], (4.5, 6.0]], dtype='interval[float64, right]')
The
closedparameter specifies which endpoints of the individualintervals within theIntervalIndexare closed.>>>pd.interval_range(end=5,periods=4,closed='both')IntervalIndex([[1, 2], [2, 3], [3, 4], [4, 5]], dtype='interval[int64, both]')