Repository: pandas-dev/pandas

Issue:
BUG: Series.get with Float64Index and numpy nan returns empty Series
I tried to use series.get(x) (where x is np.nan in some instance) and the result was an empty series.  It seems like it should just return None if it can't be found.

```
import pandas as pd
import numpy as np

series = pd.Series(range(0, 10))
series.get(np.nan) 
# returns None

series = series.reindex(pd.Float64Index(range(0, 10)))

series.get(10) 
# This correctly returns None

series.get(np.nan) 
# returns empty series with empty Float64Index

series.get(np.nan, np.nan) 
# same thing
```



Discussion:
indexing with `np.nan` on any index doesn't make much sense. what are you trying to do?

maybe `s[s.isnull()]`?

Well, it's being called like this:

```
some_series = some_series.map(lambda x: some_other_series.get(x))
```

and why would you do that? again simply use `s.isnull()` that's what it is for.

The code (I ran across the difference while regression testing, I'm not the original author) is basically trying to merge a Series into a DataFrame so the actual code was something like:

```
df[new_col] = df[lookup_col].map(lambda x: some_other_series.get(x))
```

I just noticed the behavior was inconsistent between versions (I'm going from 0.9 to 14.1).

well, that is quite inefficient, and not what you want to do anyhow, is their a reason you are not using
a) `merge`, or b) `df[lookup_col].map(some_other_series)` ?

I'm going to end up replacing it with the second option.  I work with a lot of people who write code as a part of their job, but who aren't programmers, and they don't always make the best choices.

Anyways, I thought that the result of series.get(np.nan) was unusual, but feel free to close if this isn't worth it.  Thanks.

I do appreciate the report (esp since it's an apparent change from a long ago version)

I may link this to another issue
technically a test like thks is ok

let me see


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
@@ -369,6 +369,8 @@ def get_loc(self, key, method=None, tolerance=None):
                 except (ValueError, IndexError):
                     # should only need to catch ValueError here but on numpy
                     # 1.7 .item() can raise IndexError when NaNs are present
+                    if not len(nan_idxs):
+                        raise KeyError(key)
                     return nan_idxs
         except (TypeError, NotImplementedError):
             pass


Test patch:
diff --git a/pandas/tests/indexes/test_multi.py b/pandas/tests/indexes/test_multi.py
--- a/pandas/tests/indexes/test_multi.py
+++ b/pandas/tests/indexes/test_multi.py
@@ -1172,6 +1172,14 @@ def test_get_loc_level(self):
         assert result == expected
         assert new_index.equals(index.droplevel(0))
 
+    def test_get_loc_missing_nan(self):
+        # GH 8569
+        idx = MultiIndex.from_arrays([[1.0, 2.0], [3.0, 4.0]])
+        assert isinstance(idx.get_loc(1), slice)
+        pytest.raises(KeyError, idx.get_loc, 3)
+        pytest.raises(KeyError, idx.get_loc, np.nan)
+        pytest.raises(KeyError, idx.get_loc, [np.nan])
+
     def test_slice_locs(self):
         df = tm.makeTimeDataFrame()
         stacked = df.stack()
diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py
--- a/pandas/tests/indexes/test_numeric.py
+++ b/pandas/tests/indexes/test_numeric.py
@@ -371,6 +371,14 @@ def test_get_loc_na(self):
         assert idx.get_loc(1) == 1
         pytest.raises(KeyError, idx.slice_locs, np.nan)
 
+    def test_get_loc_missing_nan(self):
+        # GH 8569
+        idx = Float64Index([1, 2])
+        assert idx.get_loc(1) == 0
+        pytest.raises(KeyError, idx.get_loc, 3)
+        pytest.raises(KeyError, idx.get_loc, np.nan)
+        pytest.raises(KeyError, idx.get_loc, [np.nan])
+
     def test_contains_nans(self):
         i = Float64Index([1.0, 2.0, np.nan])
         assert np.nan in i
diff --git a/pandas/tests/series/test_indexing.py b/pandas/tests/series/test_indexing.py
--- a/pandas/tests/series/test_indexing.py
+++ b/pandas/tests/series/test_indexing.py
@@ -70,6 +70,21 @@ def test_get(self):
         result = vc.get(True, default='Missing')
         assert result == 'Missing'
 
+    def test_get_nan(self):
+        # GH 8569
+        s = pd.Float64Index(range(10)).to_series()
+        assert s.get(np.nan) is None
+        assert s.get(np.nan, default='Missing') == 'Missing'
+
+        # ensure that fixing the above hasn't broken get
+        # with multiple elements
+        idx = [20, 30]
+        assert_series_equal(s.get(idx),
+                            Series([np.nan] * 2, index=idx))
+        idx = [np.nan, np.nan]
+        assert_series_equal(s.get(idx),
+                            Series([np.nan] * 2, index=idx))
+
     def test_delitem(self):
 
         # GH 5542