Skip to content

Commit 6412251

Browse files
dsm054TomAugspurger
authored andcommitted
BUG: Fix Series.get failure on missing NaN (#8569) (#16619)
(cherry picked from commit 2b44868)
1 parent 0e57df9 commit 6412251

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

doc/source/whatsnew/v0.20.3.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Conversion
4848
Indexing
4949
^^^^^^^^
5050

51-
51+
- Bug in ``Float64Index`` causing an empty array instead of None to be returned from ``.get(np.nan)`` on a Series whose index did not contain any NaNs (:issue:`8569`)
5252

5353
I/O
5454
^^^

pandas/core/indexes/numeric.py

+2
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,8 @@ def get_loc(self, key, method=None, tolerance=None):
369369
except (ValueError, IndexError):
370370
# should only need to catch ValueError here but on numpy
371371
# 1.7 .item() can raise IndexError when NaNs are present
372+
if not len(nan_idxs):
373+
raise KeyError(key)
372374
return nan_idxs
373375
except (TypeError, NotImplementedError):
374376
pass

pandas/tests/indexes/test_multi.py

+8
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,14 @@ def test_get_loc_level(self):
11721172
assert result == expected
11731173
assert new_index.equals(index.droplevel(0))
11741174

1175+
def test_get_loc_missing_nan(self):
1176+
# GH 8569
1177+
idx = MultiIndex.from_arrays([[1.0, 2.0], [3.0, 4.0]])
1178+
assert isinstance(idx.get_loc(1), slice)
1179+
pytest.raises(KeyError, idx.get_loc, 3)
1180+
pytest.raises(KeyError, idx.get_loc, np.nan)
1181+
pytest.raises(KeyError, idx.get_loc, [np.nan])
1182+
11751183
def test_slice_locs(self):
11761184
df = tm.makeTimeDataFrame()
11771185
stacked = df.stack()

pandas/tests/indexes/test_numeric.py

+8
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,14 @@ def test_get_loc_na(self):
371371
assert idx.get_loc(1) == 1
372372
pytest.raises(KeyError, idx.slice_locs, np.nan)
373373

374+
def test_get_loc_missing_nan(self):
375+
# GH 8569
376+
idx = Float64Index([1, 2])
377+
assert idx.get_loc(1) == 0
378+
pytest.raises(KeyError, idx.get_loc, 3)
379+
pytest.raises(KeyError, idx.get_loc, np.nan)
380+
pytest.raises(KeyError, idx.get_loc, [np.nan])
381+
374382
def test_contains_nans(self):
375383
i = Float64Index([1.0, 2.0, np.nan])
376384
assert np.nan in i

pandas/tests/series/test_indexing.py

+15
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ def test_get(self):
7070
result = vc.get(True, default='Missing')
7171
assert result == 'Missing'
7272

73+
def test_get_nan(self):
74+
# GH 8569
75+
s = pd.Float64Index(range(10)).to_series()
76+
assert s.get(np.nan) is None
77+
assert s.get(np.nan, default='Missing') == 'Missing'
78+
79+
# ensure that fixing the above hasn't broken get
80+
# with multiple elements
81+
idx = [20, 30]
82+
assert_series_equal(s.get(idx),
83+
Series([np.nan] * 2, index=idx))
84+
idx = [np.nan, np.nan]
85+
assert_series_equal(s.get(idx),
86+
Series([np.nan] * 2, index=idx))
87+
7388
def test_delitem(self):
7489

7590
# GH 5542

0 commit comments

Comments
 (0)