Repository: pandas-dev/pandas

Issue:
Checking that a pandas.Series.index contains a value
#### Code Sample, a copy-pastable example if possible

```python
# Your code here
import numpy as np
import pandas as pd

some_numbers = np.random.randint(0,4,size=10)
print(some_numbers)
s = pd.Series(some_numbers)
gb = s.groupby(s).size() / len(s)
print(gb)
1.3 in gb
```
#### Problem description
I reported it [here](https://stackoverflow.com/questions/51563433/checking-that-a-pandas-series-index-contains-a-value). The output of the last line is True instead of being False.

#### Expected Output
False

#### Output of ``pd.show_versions()``

<details>

[paste the output of ``pd.show_versions()`` here below this line]
INSTALLED VERSIONS
------------------
commit: None
python: 3.6.2.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.23.3
pytest: 3.2.1
pip: 9.0.1
setuptools: 40.0.0
Cython: 0.26.1
numpy: 1.15.0
scipy: 0.19.1
pyarrow: None
xarray: None
IPython: 6.1.0
sphinx: 1.6.3
patsy: 0.4.1
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.2
feather: None
matplotlib: 2.2.2
openpyxl: 2.4.8
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 0.9.8
lxml: 3.8.0
bs4: 4.6.0
html5lib: 0.999999999
sqlalchemy: 1.1.13
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
</details>



Discussion:
Thanks for the report!

A simpler example:

```
In [11]: 1.3 in pd.Index([0, 1, 2, 3])
Out[11]: True
```

it seems to be specific for the Int64Engine (float or object correctly give false)
The problem seems to be here:
https://github.com/pandas-dev/pandas/blob/26b3e7de56fdebaf1842b01d747a1a30a126c54a/pandas/_libs/index.pyx#L93-L96
for Int64Engine mapping is Int64HashTable
```python
>>> from pandas._libs.hashtable import Int64HashTable
>>> ht = Int64HashTable(3)
>>> ht.map_locations(np.array([1, 2, 3]))
>>> 1.1 in ht
True
```
This is because of cython casting the input float to int.

Small example showing that behaviour:

```
In [7]: %load_ext cython

In [9]: %%cython
   ...: def contains(int val):
   ...:     return val in [1, 2, 3]

In [10]: contains(1)
Out[10]: True

In [11]: contains(5)
Out[11]: False

In [12]: contains(1.3)
Out[12]: True

In [13]: contains('a')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-0aaf064190fe> in <module>()
----> 1 contains('a')

_cython_magic_32cffc084fddd25975b4666794b79f3a.pyx in _cython_magic_32cffc084fddd25975b4666794b79f3a.contains()

TypeError: an integer is required
```

Not sure if you can let cython not do such a cast on demand? (cc @jreback) 
Otherwise we should check the type of the value in/before calling the HashTable contains
these get casted to int by cython 
you need to check types with is_integer first 
Can I take this issue? I want to start contributing to this repository.
Yes, go ahead. If you have any questions related to the fix, don't hesitate to ask here.
@jorisvandenbossche thanks a lot.

Resolving patch:
diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py
--- a/pandas/core/indexes/numeric.py
+++ b/pandas/core/indexes/numeric.py
@@ -6,6 +6,7 @@
     pandas_dtype,
     needs_i8_conversion,
     is_integer_dtype,
+    is_float,
     is_bool,
     is_bool_dtype,
     is_scalar)
@@ -162,7 +163,25 @@ def insert(self, loc, item):
 )
 
 
-class Int64Index(NumericIndex):
+class IntegerIndex(NumericIndex):
+    """
+    This is an abstract class for Int64Index, UInt64Index.
+    """
+
+    def __contains__(self, key):
+        """
+        Check if key is a float and has a decimal. If it has, return False.
+        """
+        hash(key)
+        try:
+            if is_float(key) and int(key) != key:
+                return False
+            return key in self._engine
+        except (OverflowError, TypeError, ValueError):
+            return False
+
+
+class Int64Index(IntegerIndex):
     __doc__ = _num_index_shared_docs['class_descr'] % _int64_descr_args
 
     _typ = 'int64index'
@@ -220,7 +239,7 @@ def _assert_safe_casting(cls, data, subarr):
 )
 
 
-class UInt64Index(NumericIndex):
+class UInt64Index(IntegerIndex):
     __doc__ = _num_index_shared_docs['class_descr'] % _uint64_descr_args
 
     _typ = 'uint64index'


Test patch:
diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py
--- a/pandas/tests/indexing/test_indexing.py
+++ b/pandas/tests/indexing/test_indexing.py
@@ -631,6 +631,21 @@ def test_mixed_index_not_contains(self, index, val):
         # GH 19860
         assert val not in index
 
+    def test_contains_with_float_index(self):
+        # GH#22085
+        integer_index = pd.Int64Index([0, 1, 2, 3])
+        uinteger_index = pd.UInt64Index([0, 1, 2, 3])
+        float_index = pd.Float64Index([0.1, 1.1, 2.2, 3.3])
+
+        for index in (integer_index, uinteger_index):
+            assert 1.1 not in index
+            assert 1.0 in index
+            assert 1 in index
+
+        assert 1.1 in float_index
+        assert 1.0 not in float_index
+        assert 1 not in float_index
+
     def test_index_type_coercion(self):
 
         with catch_warnings(record=True):