Python Pandas - Return an IntervalArray identical to the current one but closed on the left side

PythonServer Side ProgrammingProgramming

<p>To return an IntervalArray identical to the current one but closed on the left side, use the <strong>set_closed()</strong> method with value <strong>left</strong>.</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 IntervalArray &minus;</p><pre class="just-code notranslate language-python3" data-lang="python3">index = pd.arrays.IntervalArray.from_breaks(range(5))</pre><p>Display the interval &minus;</p><pre class="just-code notranslate language-python3" data-lang="python3">print(&quot;IntervalIndex... &quot;,index)</pre><p>Return an IntervalArray identical to the current one but closed on specified side i.e. &quot;left&quot; here &minus;</p><pre class="just-code notranslate language-python3python" data-lang="python3">print(&quot; Result...&quot;,index.set_closed(&#39;left&#39;)) </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 IntervalArray index = pd.arrays.IntervalArray.from_breaks(range(5)) # Display the interval print(&quot;IntervalIndex... &quot;,index) # Display the interval length print(&quot; IntervalIndex length... &quot;,index.length) # the left bound print(&quot; The left bound for the IntervalIndex... &quot;,index.left) # the right bound print(&quot; The right bound for the IntervalIndex... &quot;,index.right) # Return an IntervalArray identical to the current one but closed on specified # side i.e. &quot;left&quot; here print(&quot; Result...&quot;,index.set_closed(&#39;left&#39;))</pre><h2>Output</h2><p>This will produce the following output &minus;</p><pre class="result notranslate">IntervalIndex... &lt;IntervalArray&gt; [(0, 1], (1, 2], (2, 3], (3, 4]] Length: 4, dtype: interval[int64, right] IntervalIndex length... Int64Index([1, 1, 1, 1], dtype=&#39;int64&#39;) The left bound for the IntervalIndex... Int64Index([0, 1, 2, 3], dtype=&#39;int64&#39;) The right bound for the IntervalIndex... Int64Index([1, 2, 3, 4], dtype=&#39;int64&#39;) Result... &lt;IntervalArray&gt; [[0, 1), [1, 2), [2, 3), [3, 4)] Length: 4, dtype: interval[int64, left]</pre>
raja
Updated on 18-Oct-2021 08:07:24

Advertisements