IntervalIndex#
- class IntervalIndex#
Immutable index backed by interval data.
Example#
import pandasCore as pd
# Create IntervalIndex
idx = pd.IntervalIndex([1, 2, 3], name='my_index')
print(len(idx)) # 3
Attributes#
Attribute |
Description |
Example |
|---|---|---|
|
String describing the inclusive side(s) of the intervals |
|
|
Return the dtype object of the underlying data |
|
|
Indicator whether IntervalIndex is empty |
|
|
Return if the index has duplicate values |
|
|
Indicates if an interval is empty (left == right and not both-closed) |
|
|
Return if the index is monotonic decreasing |
|
|
Return if the index is monotonic increasing |
|
|
Return True if intervals are non-overlapping and monotonically sorted |
|
|
Return True if any intervals overlap |
|
|
Return if the index has unique values |
|
|
Return the left bounds of the intervals |
|
|
Return the length of each interval |
|
|
Return the midpoint of each interval |
|
|
Return the name of the IntervalIndex |
|
|
Number of dimensions of the underlying data |
|
|
Number of levels in this index |
|
|
Return the right bounds of the intervals |
|
|
Return a tuple of the shape of the underlying data |
|
|
Return the number of elements in the IntervalIndex |
|
|
Vectorized string functions for Index. |
Construction#
Method |
Description |
Example |
|---|---|---|
|
Create an empty IntervalIndex |
Indexing / Selection#
Method |
Description |
Example |
|---|---|---|
|
||
|
Return new IntervalIndex with elements at specified positions |
|
|
Replace values where the condition is False. |
Data Manipulation#
Method |
Description |
Example |
|---|---|---|
|
Make new Index with passed list of labels deleted |
|
|
Return index with requested level(s) removed. |
|
|
Make new IntervalIndex inserting new item at location |
|
|
Conform Index to new index with optional filling logic |
|
|
Alter IntervalIndex name. |
|
|
Set IntervalIndex name |
Missing Data#
Method |
Description |
Example |
|---|---|---|
|
Return IntervalIndex without NA values |
|
|
Fill NA/NaN values with the specified value |
|
|
Detect missing values |
|
|
Detect missing values (alias for isna) |
|
|
Detect existing (non-missing) values |
|
|
Detect existing (non-missing) values (alias for notna) |
Statistics#
Method |
Description |
Example |
|---|---|---|
|
Return the maximum interval (by left bound) |
|
|
Return the minimum interval (by left bound) |
|
|
Return number of unique elements in the index |
|
|
Return a dict containing counts of unique values |
Aggregation#
Method |
Description |
Example |
|---|---|---|
|
Group the index labels by a given array of values |
|
|
Map values using an input mapping or function |
Comparison#
Method |
Description |
Example |
|---|---|---|
|
Return self==value. |
|
|
Return self>=value. |
|
|
Return self>value. |
|
|
Return self<=value. |
|
|
Return self<value. |
|
|
Return self!=value. |
|
|
Determine if two IntervalIndex objects are equal |
Sorting#
Method |
Description |
Example |
|---|---|---|
|
Return the indices that would sort the index |
|
|
Find indices where elements should be inserted to maintain order |
|
|
Return a sorted copy of the index |
Reshaping#
Method |
Description |
Example |
|---|---|---|
|
Create a DataFrame with a column containing the IntervalIndex values |
|
|
Return the transpose (self for 1D index) |
Combining#
Method |
Description |
Example |
|---|---|---|
|
Append another IntervalIndex to this one |
|
|
Join this index with another |
Time Series#
Method |
Description |
Example |
|---|---|---|
|
Return the label from the index, or, if not present, the previous one. |
|
|
Compute first-order differences |
|
|
Shift index by desired number of periods |
I/O#
Method |
Description |
Example |
|---|---|---|
|
Return a flattened index |
|
|
Return a list of the intervals as tuples |
|
|
A NumPy ndarray representing the values in this Series or Index |
|
|
Create a Series with the IntervalIndex values |
|
|
Return a list of tuples of the form (left, right) |
|
|
Return a list of the intervals as tuples |
Conversion#
Method |
Description |
Example |
|---|---|---|
|
Cast to a specified dtype. |
|
|
Make a copy of this IntervalIndex. |
|
|
Infer objects type and return a converted version. |
|
|
Return a view of the index. |
Iteration#
Method |
Description |
Example |
|---|---|---|
|
||
|
||
|
Set Operations#
Method |
Description |
Example |
|---|---|---|
|
Form the set difference of two IntervalIndex objects |
|
|
Return IntervalIndex with duplicate values removed |
|
|
Indicate duplicate index values |
|
|
Form the intersection of two IntervalIndex objects |
|
|
Check if values are in the IntervalIndex |
|
|
Form the symmetric difference of two IntervalIndex objects |
|
|
Form the union of two IntervalIndex objects |
|
|
Return unique values in the index |
String Methods#
Method |
Description |
Example |
|---|---|---|
|
Check elementwise if the Intervals contain the value. |
Datetime Methods#
Method |
Description |
Example |
|---|---|---|
|
Round interval bounds to specified number of decimals |
Other Methods#
Method |
Description |
Example |
|---|---|---|
|
Return hash(self). |
|
|
||
|
||
|
Return whether all elements are truthy |
|
|
Return whether any element is truthy |
|
|
Return the index of the maximum value |
|
|
Return the index of the minimum value |
|
|
Return the locations (indices) of labels in the index. |
|
|
Make new IntervalIndex with element at position deleted |
|
|
Encode the object as an enumerated type or categorical variable |
|
|
Format the IntervalIndex as list of strings |
|
|
Compute indexer for new index given the current index |
|
|
Guaranteed return of an indexer even when non-unique |
|
|
Compute indexer and mask for new index given the current index. |
|
|
Return an IntervalIndex of values for requested level |
|
|
Get integer location for requested label |
|
|
Calculate slice bound that corresponds to given label. |
|
|
Check if the index holds integers (always False for float64 Interva… |
|
|
Determine if two IntervalIndex objects are identical |
|
|
Return the first element as a scalar (only for single-element index) |
|
|
Return memory usage of the index in bytes |
|
|
Check elementwise if an interval overlaps the values. |
|
|
Return a new IntervalIndex with values put where mask is True. |
|
|
Return a flattened view of the index. |
|
|
Repeat elements of the index |
|
|
Return an IntervalIndex with the specified closed side. |
|
|
Compute slice indexer for input labels. |
|
|
Compute slice locations for input labels |
|
|
Return a sorted copy of the index. |
|
|
Sort index by level |
Code Examples#
The following examples are extracted from the test suite.
str (test_interval_multi_range_str.py:79)
69def test_intervalindex_str_call_with_data():
70 """Test IntervalIndex.str(data) call."""
71 print("Testing IntervalIndex.str(data) call...")
72
73 # Create an IntervalIndex
74 ii = pandasCore.IntervalIndex.from_breaks([0, 1, 2])
75
76 # Call IntervalIndex.str with the index as argument
77 try:
78 str_accessor = pandasCore.IntervalIndex.str(ii)
79 print(f" IntervalIndex.str(ii) returned: {type(str_accessor)}")
80 print(" PASSED: IntervalIndex.str(data) call works")
81 except Exception as e:
82 # Expected to raise error since IntervalIndex doesn't contain strings
83 print(f" Error (expected for non-string data): {e}")
84 print(" PASSED: Appropriate error raised for non-string Index")
85
86# =============================================================================
87# MultiIndex.str tests
88# =============================================================================
__init__ (test_intervalindex.py:87)
77 ctx.assert_equal((0.0, 1.0), ii[0], "from_tuples first interval")
78 ctx.assert_equal((2.0, 3.0), ii[1], "from_tuples second interval")
79 ctx.assert_equal((4.0, 5.0), ii[2], "from_tuples third interval")
80
81
82def test_constructor():
83 """Test IntervalIndex constructor with list of tuples"""
84 f_print_header("Test: constructor")
85
86 data = [(0.0, 1.0), (1.0, 2.0), (2.0, 3.0)]
87 ii = pandasCore.IntervalIndex(data)
88
89 ctx.assert_equal(3, ii.size, "constructor size")
90 ctx.assert_equal("right", ii.closed, "constructor closed")
91
92 # With closed parameter
93 ii_both = pandasCore.IntervalIndex(data, closed="both")
94 ctx.assert_equal("both", ii_both.closed, "constructor closed=both")
95
96
97def test_properties():