
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check elementwise if the Intervals in the IntervalIndex contain the value in Python Pandas
To return an IntervalArray identical to the current one but closed on the specified side, use the set_closed() method with parameter set as both.
At first, import the required libraries −
import pandas as pd
Create IntervalArray −
index = pd.arrays.IntervalArray.from_breaks(range(6))
Display the interval −
print("IntervalIndex...\n",index)
Return an IntervalArray identical to the current one but closed on specified side i.e. "both" here −
print("\nResult...",index.set_closed('both'))
Example
Following is the code −
import pandas as pd # Create IntervalArray index = pd.arrays.IntervalArray.from_breaks(range(6)) # Display the interval print("IntervalIndex...\n",index) # Display the interval length print("\nIntervalIndex length...\n",index.length) # the left bound print("\nThe left bound for the IntervalIndex...\n",index.left) # the right bound print("\nThe right bound for the IntervalIndex...\n",index.right) # Return an IntervalArray identical to the current one but closed on specified # side i.e. "both" here print("\nResult...",index.set_closed('both'))
Output
This will produce the following output −
IntervalIndex... <IntervalArray> [(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]] Length: 5, dtype: interval[int64, right] IntervalIndex length... Int64Index([1, 1, 1, 1, 1], dtype='int64') The left bound for the IntervalIndex... Int64Index([0, 1, 2, 3, 4], dtype='int64') The right bound for the IntervalIndex... Int64Index([1, 2, 3, 4, 5], dtype='int64') Result... <IntervalArray> [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5]] Length: 5, dtype: interval[int64, both]
- Related Articles
- Python Pandas - Check elementwise if the Intervals in the IntervalIndex contain the value
- Python Pandas - Check elementwise if the Intervals contain the value
- Python Pandas - Check if the IntervalIndex has overlapping intervals
- Python Pandas IntervalIndex - Check if Intervals that share closed endpoints overlap
- Check elementwise if an Interval overlaps the values in the IntervalArray in Python Pandas
- Python Pandas - Check if the Intervals in the IntervalArray is empty
- Python Pandas IntervalIndex - Check if Intervals that only have an open endpoint in common overlap or not
- Python Pandas IntervalIndex - Get the locations of all the relevant interval if a label is in several intervals
- Python Pandas - Check whether the IntervalIndex intervals are closed on the left-side, right-side, both or neither
- Python Pandas - Check elementwise if an Interval overlaps the values in the IntervalArray created from an array of splits
- Python Pandas - Get the midpoint from the IntervalIndex
- Python Pandas - Get the length from the IntervalIndex
- Python Pandas - Get the left bound for the IntervalIndex
- Python Pandas - Get the right bound for the IntervalIndex
- Python Pandas IntervalIndex - Check if an interval that contains points is empty or not

Advertisements