Repository: pandas-dev/pandas

Issue:
BUG: IntervalIndex.get_loc fails when there is only one entry
#### Code Sample, a copy-pastable example if possible

```python
>>> import pandas as pd
>>> pd.IntervalIndex.from_tuples([(1,100)]).get_loc(50)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/imre/code/pandas/pandas/core/indexes/interval.py", line 1049, in get_loc
    raise KeyError(original_key)
KeyError: 50
```
#### Problem description
50 is contained in the interval (1, 100), so this should not raise `KeyError`.

#### Expected Output
0

#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------
commit: c4da79b5b322c73d8e61d1cb98ac4ab1e2438b40
python: 3.6.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.13.0-39-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.0.dev0+824.gc4da79b5b
pytest: None
pip: 10.0.1
setuptools: 39.1.0
Cython: 0.28.2
numpy: 1.14.3
scipy: None
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.7.2
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

</details>



Discussion:
xref https://github.com/pandas-dev/pandas/issues/17284#issuecomment-325890615

Not exactly a dupe of that issue, but the same fix will probably resolve both of these.

Resolving patch:
diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py
--- a/pandas/core/indexes/interval.py
+++ b/pandas/core/indexes/interval.py
@@ -159,20 +159,22 @@ class IntervalIndex(IntervalMixin, Index):
 
     Attributes
     ----------
-    left
-    right
     closed
-    mid
+    is_non_overlapping_monotonic
+    left
     length
+    mid
+    right
     values
-    is_non_overlapping_monotonic
 
     Methods
     -------
+    contains
     from_arrays
-    from_tuples
     from_breaks
-    contains
+    from_tuples
+    get_indexer
+    get_loc
 
     Examples
     ---------
@@ -938,8 +940,11 @@ def _searchsorted_monotonic(self, label, side, exclude_label=False):
         if isinstance(label, IntervalMixin):
             raise NotImplementedError
 
+        # GH 20921: "not is_monotonic_increasing" for the second condition
+        # instead of "is_monotonic_decreasing" to account for single element
+        # indexes being both increasing and decreasing
         if ((side == 'left' and self.left.is_monotonic_increasing) or
-                (side == 'right' and self.left.is_monotonic_decreasing)):
+                (side == 'right' and not self.left.is_monotonic_increasing)):
             sub_idx = self.right
             if self.open_right or exclude_label:
                 label = _get_next_label(label)


Test patch:
diff --git a/pandas/tests/indexes/interval/test_interval.py b/pandas/tests/indexes/interval/test_interval.py
--- a/pandas/tests/indexes/interval/test_interval.py
+++ b/pandas/tests/indexes/interval/test_interval.py
@@ -497,6 +497,14 @@ def test_get_loc_interval(self):
         pytest.raises(KeyError, self.index.get_loc,
                       Interval(-1, 0, 'left'))
 
+    # Make consistent with test_interval_new.py (see #16316, #16386)
+    @pytest.mark.parametrize('item', [3, Interval(1, 4)])
+    def test_get_loc_length_one(self, item, closed):
+        # GH 20921
+        index = IntervalIndex.from_tuples([(0, 5)], closed=closed)
+        result = index.get_loc(item)
+        assert result == 0
+
     # To be removed, replaced by test_interval_new.py (see #16316, #16386)
     def test_get_indexer(self):
         actual = self.index.get_indexer([-1, 0, 0.5, 1, 1.5, 2, 3])
@@ -544,6 +552,16 @@ def test_get_indexer_subintervals(self):
         expected = np.array([0, 0, 0], dtype='intp')
         tm.assert_numpy_array_equal(actual, expected)
 
+    # Make consistent with test_interval_new.py (see #16316, #16386)
+    @pytest.mark.parametrize('item', [
+        [3], np.arange(1, 5), [Interval(1, 4)], interval_range(1, 4)])
+    def test_get_indexer_length_one(self, item, closed):
+        # GH 17284
+        index = IntervalIndex.from_tuples([(0, 5)], closed=closed)
+        result = index.get_indexer(item)
+        expected = np.array([0] * len(item), dtype='intp')
+        tm.assert_numpy_array_equal(result, expected)
+
     # To be removed, replaced by test_interval_new.py (see #16316, #16386)
     def test_contains(self):
         # Only endpoints are valid.