search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas | interval_range method

schedule Aug 12, 2023
Last updated
local_offer
PandasPython
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Pandas interval_range(~) method returns an IntervalIndex with fixed frequency.

NOTE

If you are dealing with time-related data, then opt to use period_range(~) instead.

Parameters

1. startlink | numeric or datetime-like | optional

The lower bound of the range. By default, start=None.

2. endlink | numeric or datetime-like | optional

The upper bound of the range. By default, end=None.

3. periodslink | int | optional

The desired number of dates. By default, periods=True.

WARNING

Out of the above three parameters, exactly two must be specified - no less, no more.

4. freqlink | 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. namelink | string | optional

The name assigned to the resulting IntervalIndex. By default, name=None.

6. closedlink | string | optional

Whether or not to make the bounds inclusive/exclusive:

Value

Description

"left"

  • Left endpoint is inclusive.

  • Right endpoint is exclusive.

"right"

  • Left endpoint is exclusive.

  • Right endpoint is inclusive.

"both"

Both endpoints are inclusive.

"neither"

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):

pd.interval_range(start=pd.Timestamp("2020-12-25"), end=pd.Timestamp("2020-12-28"))
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]]')

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):

pd.interval_range(start=pd.Timestamp("2020-12-25"), periods=3)
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:

pd.interval_range(start=pd.Timestamp("2020-12-25"), periods=3)   # freq="D"
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:

pd.interval_range(start=pd.Timestamp("2020-12-25"), periods=3, freq="2D")
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:

pd.interval_range(start=pd.Timestamp("2020-12-25"), periods=3, name="My Dates")
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"):

pd.interval_range(start=pd.Timestamp("2020-12-25"), periods=3)   # 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":

pd.interval_range(start=pd.Timestamp("2020-12-25"), periods=3, 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":

pd.interval_range(start=pd.Timestamp("2020-12-25"), periods=3, 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":

pd.interval_range(start=pd.Timestamp("2020-12-25"), periods=3, 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)
pd.DataFrame({"A":[3,4],"B":[5,6]}, index=idx)
A B
(5, 6] 3 5
(6, 7] 4 6
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!