BUG: TimedeltaIndex indexing regression for strings in v0.20.3 · Issue #16896 · pandas-dev/pandas (original) (raw)

In [1]: import pandas as pd

In [3]: df = pd.DataFrame({'x': range(3)}, pd.to_timedelta(range(3), unit='days'))

In [4]: df.loc['0 days']
Out[4]:
x    0
Name: 0 days 00:00:00, dtype: int64

In [5]: df.index
Out[5]: TimedeltaIndex(['0 days', '1 days', '2 days'], dtype='timedelta64[ns]', freq=None)

In [7]: pd.__version__
Out[7]: '0.20.2'
In [1]: import pandas as pd

In [2]: df = pd.DataFrame({'x': range(3)}, pd.to_timedelta(range(3), unit='days'))

In [3]: df.loc['0 days']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/Users/shoyer/conda/envs/xarray-dev/lib/python3.5/site-packages/pandas/core/indexing.py in _has_valid_type(self, key, axis)
   1433                 if not ax.contains(key):
-> 1434                     error()
   1435             except TypeError as e:

/Users/shoyer/conda/envs/xarray-dev/lib/python3.5/site-packages/pandas/core/indexing.py in error()
   1428                 raise KeyError("the label [%s] is not in the [%s]" %
-> 1429                                (key, self.obj._get_axis_name(axis)))
   1430

KeyError: 'the label [0 days] is not in the [index]'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-3-17caa55b0d41> in <module>()
----> 1 df.loc['0 days']

/Users/shoyer/conda/envs/xarray-dev/lib/python3.5/site-packages/pandas/core/indexing.py in __getitem__(self, key)
   1326         else:
   1327             key = com._apply_if_callable(key, self.obj)
-> 1328             return self._getitem_axis(key, axis=0)
   1329
   1330     def _is_scalar_access(self, key):

/Users/shoyer/conda/envs/xarray-dev/lib/python3.5/site-packages/pandas/core/indexing.py in _getitem_axis(self, key, axis)
   1549
   1550         # fall thru to straight lookup
-> 1551         self._has_valid_type(key, axis)
   1552         return self._get_label(key, axis=axis)
   1553

/Users/shoyer/conda/envs/xarray-dev/lib/python3.5/site-packages/pandas/core/indexing.py in _has_valid_type(self, key, axis)
   1440                 raise
   1441             except:
-> 1442                 error()
   1443
   1444         return True

/Users/shoyer/conda/envs/xarray-dev/lib/python3.5/site-packages/pandas/core/indexing.py in error()
   1427                                     "key")
   1428                 raise KeyError("the label [%s] is not in the [%s]" %
-> 1429                                (key, self.obj._get_axis_name(axis)))
   1430
   1431             try:

KeyError: 'the label [0 days] is not in the [index]'

This turned up in a failure for the xarray test suite.