Pandas | IntervalIndex constructor
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.
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 |
|---|---|
|
|
|
|
| Make both endpoints inclusive. |
| 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, thendatais copied to construct a newIntervalIndex. Modifying the returnedIntervalIndexwill not mutatedata.If
False, then no copying is done so that the returnedIntervalIndexwill hold a reference to data. Modifying theIntervalIndexwill mutatedata, 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:
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:
Amy_intervals (3, 4] a(4, 6] b