title: What is the difference between unique index and monotonic index?
body: <p>I'm trying to use <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Index.get_loc.html" rel="nofollow noreferrer">pandas.Index.get_loc</a> to return the index (as an <code>int</code>) of the nearest value, but occasionally it returns a <code>slice</code> object instead. According to the documentation, </p>
  
  <blockquote>
    <p>get_loc returns int if unique index, slice if monotonic index, else mask.</p>
  </blockquote>
  
  <p>But it doesn't look like the behavior is consistent. For example, with the following index:</p>
  
  <pre><code>idx = pd.DatetimeIndex(['2019-12-24 12:04:54',
                          '2019-12-26 20:09:22',
                          '2020-12-27 07:44:35'])
  </code></pre>
  
  <p>Using <code>idx.get_loc('2019-12-27', method='ffill')</code> returns <code>slice(2, 2, None)</code>, whereas <code>idx.get_loc('2019-12-29', method='ffill')</code> returns <code>2</code>. Changing the method from <code>'ffill'</code> to <code>'bfill'</code> doesn't seem to change the result.</p>
  
  <p>My aim is to slice all points from the beginning of the index like <code>idx[:i]</code> where <code>i</code> is an int returned by <code>get_loc</code>. Another solution might to modify the beginning of the <code>slice</code> object, if that is possible.</p>
  
  <p><strong>Edit:</strong>
  Apparently, a <code>slice</code> is a built-in object with <em>read-only</em> data attributes start, stop and step (see docs <a href="https://docs.python.org/3/library/functions.html#slice" rel="nofollow noreferrer">here</a>). This means you can check whether the result of <code>get_loc</code> is an <code>int</code> and if not, use <code>idx[:slice.stop]</code> to get all elements up to the desired index.</p>
  
  <p>I'm still interested in the original question though.</p>