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

PythonServer Side ProgrammingProgramming

<p>To check whether the intervals in IntervalArray are closed on the left-side, right-side, both or neither, use the <strong>array.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 two Interval objects. Closed intervals set using the &quot;closed&quot; parameter with value &quot;both&quot;. A closed interval (in mathematics denoted by square brackets) contains its endpoints, i.e. the closed interval [0, 5] is characterized by the conditions 0 &lt;= x &lt;= 5 &minus;</p><pre class="just-code notranslate language-python3" data-lang="python3">interval1 = pd.Interval(10, 25, closed=&#39;both&#39;) interval2 = pd.Interval(15, 70, closed=&#39;both&#39;)</pre><p>Display the intervals &minus;</p><pre class="just-code notranslate language-python3" data-lang="python3">print(&quot;Interval1... &quot;,interval1) print(&quot;Interval2... &quot;,interval2)</pre><p>Construct a new IntervalArray from Interval objects &minus;</p><pre class="just-code notranslate language-python3" data-lang="python3">array = pd.arrays.IntervalArray([interval1,interval2]) </pre><p>Check whether the intervals in the Interval Array is closed on the left-side, right-side, both or neither &minus;</p><pre class="just-code notranslate language-python3" data-lang="python3">print(&quot; Checking whether the intervals is closed... &quot;,array.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 two Interval objects # Closed intervals set using the &quot;closed&quot; parameter with value &quot;both&quot; # A closed interval (in mathematics denoted by square brackets) contains its endpoints, # i.e. the closed interval [0, 5] is characterized by the conditions 0 &lt;= x &lt;= 5 interval1 = pd.Interval(10, 25, closed=&#39;both&#39;) interval2 = pd.Interval(15, 70, closed=&#39;both&#39;) # display the intervals print(&quot;Interval1... &quot;,interval1) print(&quot;Interval2... &quot;,interval2) # Construct a new IntervalArray from Interval objects array = pd.arrays.IntervalArray([interval1,interval2]) # Display the IntervalArray print(&quot; Our IntervalArray... &quot;,array) # Getting the length of IntervalArray # Returns an Index with entries denoting the length of each Interval in the IntervalArray print(&quot; Our IntervalArray length... &quot;,array.length) # check whether the intervals in the Interval Array is closed on the left-side, right-side, # both or neither print(&quot; Checking whether the intervals is closed... &quot;,array.closed)</pre><h2>Output</h2><p>This will produce the following code &minus;</p><pre class="result notranslate">Interval1... [10, 25] Interval2... [15, 70] Our IntervalArray... &lt;IntervalArray&gt; [[10, 25], [15, 70]] Length: 2, dtype: interval[int64, both] Our IntervalArray length... Int64Index([15, 55], dtype=&#39;int64&#39;) Checking whether the intervals is closed... Both</pre>
raja
Updated on 12-Oct-2021 12:29:50

Advertisements