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 | IntervalIndex constructor

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

Pandas IntervalIndex(~) constructor is used to initialize an IntervalIndex object, which can be used as the index of a Series or a DataFrame.

WARNING

To initialize an IntervalIndex, it is highly recommended that you use pd.interval_range(~) instead of calling this constructor directly since the method is much easier to use.

Parameters

1. data | 1D array-like of Interval

The data from which to construct the IntervalIndex.

2. closed | string | optional

Whether to make the endpoints inclusive or exclusive:

Value

Description

"left"

  • Make the left endpoint inclusive.

  • Make the right endpoint exclusive.

"right"

  • Make the right endpoint inclusive.

  • Make the left endpoint exclusive.

"both"

Make both endpoints inclusive.

"neither"

Make both endpoints exclusive.

By default, closed="right".

3. dtype | dtype or None | optional

The data type to use for the IntervalIndex. By default, the data type will be inferred from data.

4. copy | boolean | optional

  • If True, then data is copied to construct a new IntervalIndex. Modifying the returned IntervalIndex will not mutate data.

  • If False, then no copying is done so that the returned IntervalIndex will hold a reference to data. Modifying the IntervalIndex will mutate data, and vice versa.

By default, copy=False.

5. namelink | object | optional

The name assigned to the Interval Index.

6. verify_integrity | boolean | optional

Whether or not to raise an error if the resulting index is invalid. By default, verify_integrity=True.

Return Value

A IntervalIndex.

Examples

Basic usage

To create an IntervalIndex:

i1 = pd.Interval(left=3, right=4)
i2 = pd.Interval(left=4, right=6)
idx = pd.IntervalIndex([i1,i2])
idx
IntervalIndex([(3, 4], (4, 6]],
closed='right',
dtype='interval[int64]')

To use this as the index of a DataFrame:

pd.DataFrame({"A":["a","b"]}, index=idx)
A
(3, 4] a
(4, 6] b

Here, (3,4] denotes the interval 3 < x <= 4.

Specifying name

To assign a name to the IntervalIndex, specify name like so:

i1 = pd.Interval(left=3, right=4)
i2 = pd.Interval(left=4, right=6)
idx = pd.IntervalIndex([i1,i2], name="my_intervals")
idx
IntervalIndex([(3, 4], (4, 6]],
closed='right',
name='my_intervals',
dtype='interval[int64]')

When assigning this as the index of a DataFrame:

pd.DataFrame({"A":["a","b"]}, index=idx)
A
my_intervals
(3, 4] a
(4, 6] b
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
1
thumb_down
1
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!