In [195]: df = pd.DataFrame(np.random.randn(14), index=np.arange(138.915, 140.0, 0.08),
columns=['noise'])
In [206]: df
Out[206]:
noise
138.915 -1.474675
138.995 0.768455
139.075 0.154067
139.155 1.201179
139.235 -0.554067
139.315 -0.713285
139.395 0.643849
139.475 0.601655
139.555 1.331853
139.635 -0.900771
139.715 0.823289
139.795 -0.604716
139.875 -0.625836
139.955 0.604149
In [196]: df.index
Out[196]:
Float64Index([138.915, 138.995, 139.075, 139.155, 139.235, 139.315, 139.395,
139.475, 139.555, 139.635, 139.715, 139.795, 139.875, 139.955],
dtype='float64')
If I have the above data frame, label based slicing only work for the first two index.
In [204]: df.loc[138.915]
Out[204]:
noise -1.474675
Name: 138.915, dtype: float64
In [202]: df.loc[138.995]
Out[202]:
noise 0.768455
Name: 138.995, dtype: float64
In [205]: df.loc[139.075]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/data2/piyanat/src/miniconda3/lib/python3.5/site-packages/pandas/core/indexing.py in _has_valid_type(self, key, axis)
1382 if key not in ax:
-> 1383 error()
1384 except TypeError as e:
/data2/piyanat/src/miniconda3/lib/python3.5/site-packages/pandas/core/indexing.py in error()
1377 raise KeyError("the label [%s] is not in the [%s]" %
-> 1378 (key, self.obj._get_axis_name(axis)))
1379
KeyError: 'the label [139.075] is not in the [index]'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-205-069cc26fcd45> in <module>()
----> 1 df.loc[139.075]
/data2/piyanat/src/miniconda3/lib/python3.5/site-packages/pandas/core/indexing.py in __getitem__(self, key)
1284 return self._getitem_tuple(key)
1285 else:
-> 1286 return self._getitem_axis(key, axis=0)
1287
1288 def _getitem_axis(self, key, axis=0):
/data2/piyanat/src/miniconda3/lib/python3.5/site-packages/pandas/core/indexing.py in _getitem_axis(self, key, axis)
1427
1428 # fall thru to straight lookup
-> 1429 self._has_valid_type(key, axis)
1430 return self._get_label(key, axis=axis)
1431
/data2/piyanat/src/miniconda3/lib/python3.5/site-packages/pandas/core/indexing.py in _has_valid_type(self, key, axis)
1389 raise
1390 except:
-> 1391 error()
1392
1393 return True
/data2/piyanat/src/miniconda3/lib/python3.5/site-packages/pandas/core/indexing.py in error()
1376 "key")
1377 raise KeyError("the label [%s] is not in the [%s]" %
-> 1378 (key, self.obj._get_axis_name(axis)))
1379
1380 try:
KeyError: 'the label [139.075] is not in the [index]'
But range slicing works.
In [207]: df.loc[139.3:139.8]
Out[207]:
noise
139.315 -0.713285
139.395 0.643849
139.475 0.601655
139.555 1.331853
139.635 -0.900771
139.715 0.823289
139.795 -0.604716
output of pd.show_versions()
In [209]: pd.show_versions()
INSTALLED VERSIONS
------------------
commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Linux
OS-release: 3.5.0-41-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: C
LANG: en_US.UTF-8
pandas: 0.18.0
nose: None
pip: 8.1.1
setuptools: 20.6.7
Cython: None
numpy: 1.10.4
scipy: 0.17.0
statsmodels: None
xarray: None
IPython: 4.1.2
sphinx: None
patsy: None
dateutil: 2.5.2
pytz: 2016.3
blosc: None
bottleneck: None
tables: 3.2.2
numexpr: 2.5.2
matplotlib: 1.5.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
boto: None
Comment From: jreback
This is not a bug, but the limits of float precision. Not sure how you can exactly match a label. So with a float index you must use a slice, or you can do something like this to specify precision (this works at the default, just showing how)
In [18]: df.reindex([139.075],method='nearest',tolerance=.0001)
Out[18]:
noise
139.075 0.604928
I suppose you could have a doc-update here to show an example of how to do this
this will also be addressed at some point by: https://github.com/pydata/pandas/issues/9817