title: Pandas loc uses inclusive ranges and iloc uses exclusive on upper side
body: <p>There are myriad posts, along with <code>pandas</code>' own warnings, about performing slices of slices, something like:</p>
  
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; df['b'][1]
  </code></pre>
  
  <p>So, the better practice is:</p>
  
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; df.loc[1,'b']
  </code></pre>
  
  <p>But I (we) often have to ignore the actual <em>values</em> of row indices because some actions can alter or recreate them.  So when writing functions or methods, I want to use <code>loc</code> because names are easier for columns, but <code>iloc</code> to deal with this inconsistency of row names.</p>
  
  <hr>
  
  <p>I thought a nice compromise would be:</p>
  
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; df.loc[df.index[1],'b']
  </code></pre>
  
  <p>That works. But then I noticed this strange issue with indexing:</p>
  
  <h1>Slices are Inconsistent Between <code>loc</code> and <code>iloc</code>!</h1>
  
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; df = pd.DataFrame({'b': [4.2, 8.8, 1, 1], 'c': [5, 10, 2, 9]})
  &gt;&gt;&gt; df
       b   c
  0  4.2   5
  1  8.8  10
  2  1.0   2
  3  1.0   9
  
  &gt;&gt;&gt; df.loc[df.index[1:2],:]
       b   c
  1  8.8  10
  &gt;&gt;&gt; df.loc[1:2,:]
       b   c
  1  8.8  10
  2  1.0   2
  </code></pre>
  
  <p>I was very confused by this, until I realized:</p>
  
  <ol>
  <li><p><code>iloc</code> is <em>exclusive</em> in ranges, like most of Python.  Eg,</p>
  
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; df.iloc[1:2,:]
       b   c
  1  8.8  10
  </code></pre></li>
  <li><p><code>loc</code> is <em>inclusive</em> in ranges, capturing all "names" provided.  Eg,</p>
  
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; df.loc[1:2,:]
   b   c
  1  8.8  10
  2  1.0   2
  </code></pre></li>
  <li><code>Series</code>' <code>index</code> acts like <code>iloc</code>, being <em>exclusive</em>.</li>
  </ol>
  
  <h1>What to Do?</h1>
  
  <p>I now know this, and hopefully will not screw up again.</p>
  
  <p><strong><em>But is this the right behavior</em></strong>?</p>
  
  <p>I can post to <code>pandas</code>' GitHub bugs, but I wanted to hear why what seems so strange to me might have good logic.</p>
  
  <p>I understand that ranges are typically exclusive, and that being exclusive on <em>names</em> is weird.  </p>
  
  <p>But maybe there is a better third alternative, like a <strong><em>version of <code>loc</code> that allows one to specify that the rows are numeric (<code>iloc</code>-like) and the columns are names</em></strong>?  After all, this is the behavior that we all want 95% of the time.</p>