Code Sample, a copy-pastable example if possible
df = pd.DataFrame(
{
"a":[11, 11, 22],
"i":[1081., 1071., 22.],
},
)
df = df.set_index("i")
# df.index = df.set_index("i").astype(int)
print df.loc[[-99999999]]
Problem description
The above gets:
a
i
-99999999 NaN
... where did this row come from? Was not in my data.
Expected Output
`KeyError: u'None of [[-99999999]] are in the [index]'
Output of pd.show_versions()
Comment From: jschendel
To expand slightly: this only appears to be an issue with Float64Index
, other indexes appear to be behaving properly.
The FutureWarning for partial matches also does not appear to be working for Float64Index
:
In [2]: s = pd.Series(['foo', 'bar', 'baz'], index=pd.Float64Index([0, 1, 2]))
In [3]: s
Out[3]:
0.0 foo
1.0 bar
2.0 baz
dtype: object
In [4]: s.loc[[2.0, 3.0]]
Out[4]:
2.0 baz
3.0 NaN
dtype: object
Whereas the FutureWarning works for other types of index, e.g. RangeIndex
:
In [5]: s2 = pd.Series(['foo', 'bar', 'baz'])
In [6]: s2
Out[6]:
0 foo
1 bar
2 baz
dtype: object
In [7]: s2.loc[[2, 3]]
FutureWarning:
Passing list-likes to .loc or [] with any missing label will raise
KeyError in the future, you can use .reindex() as an alternative.
See the documentation here:
https://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate-loc-reindex-listlike
if __name__ == '__main__':
Out[7]:
2 baz
3 NaN
dtype: object
Comment From: alexcwatt
Using the copy/paste example in the original issue description, I am unable to reproduce this on master with Python 3.6.6; I see the expected KeyError
raised.
Comment From: WillAyd
@alexcwatt can you see if we have a test for this? If not would take a PR to add one
Comment From: phofl
test_loc_getitem_label_out_of_range
in pandas/tests/indexing/test_loc.py
covers this.