Python Pandas - Check whether the IntervalIndex intervals are closed on the left-side, right-side, both or neither

PythonServer Side ProgrammingProgramming

<p>To check whether the IntervalIndex intervals are closed on the left-side, right-side, both or neither, use the <strong>interval.closed</strong> property.</p><p>At first, import the required libraries &minus;</p><pre class="just-code notranslate language-python3" data-lang="python3">import pandas as pd</pre><p>Create IntervalIndex &minus;</p><pre class="just-code notranslate language-python3" data-lang="python3">interval = pd.IntervalIndex.from_arrays([5, 10, 15], [15, 20, 25]) </pre><p>Display the interval &minus;</p><pre class="just-code notranslate language-python3" data-lang="python3">print(&quot;IntervalIndex... &quot;,interval)</pre><p>Check whether the IntervalIndex is closed on the left-side, right-side, both or neither &minus;</p><pre class="just-code notranslate language-python3python" data-lang="python3">print(&quot; Checking for the type of IntervalIndex... &quot;,interval.closed) </pre><h2>Example</h2><p>Following is the code &minus;</p><pre class="demo-code notranslate language-python3" data-lang="python3">import pandas as pd # Create IntervalIndex interval = pd.IntervalIndex.from_arrays([5, 10, 15], [15, 20, 25]) # Display the interval print(&quot;IntervalIndex... &quot;,interval) # Display the interval length print(&quot; IntervalIndex length... &quot;,interval.length) # Check whether the IntervalIndex is closed on the left-side, right-side, both or neither print(&quot; Checking for the type of IntervalIndex... &quot;,interval.closed)</pre><h2>Output</h2><p>This will produce the following output &minus;</p><pre class="result notranslate">IntervalIndex... IntervalIndex([(5, 15], (10, 20], (15, 25]], dtype=&#39;interval[int64, right]&#39;) IntervalIndex length... Int64Index([10, 10, 10], dtype=&#39;int64&#39;) Checking for the type of IntervalIndex... Right</pre>
raja
Updated on 18-Oct-2021 07:27:19

Advertisements