BUG: TimedeltaIndex raising ValueError when slice indexing · Issue #16637 · pandas-dev/pandas (original) (raw)
This is tangentially related to #14946.
Code Sample, a copy-pastable example if possible
import pandas as pd
df = pd.DataFrame({'x': range(10)}) df.index = pd.to_timedelta(df.index, unit='s')
Each of these lines work as intended
df.loc[df.index[0], 'x'] = 10 df.loc[df['x'] > 1, 'x'] = 20 df.iloc[[0, 2], 0] = 30 df.iloc[2:4, 0] = 40
These lines fail however
df.loc[df.index[7:9], 'x'] = 50 df.loc[df.index[3:5], 'x'] = 60
Problem description
This behavior is broken since 0.19.1 and is still occurring with the latest version of pandas. The problem is pretty self-explanatory.
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Expected Output
The output is self-explanatory.
It can be easily fixed by adding the following to core/indexes/timedeltas.py TimedeltaIndex:
def get_loc(self, key, method=None, tolerance=None): if is_bool_indexer(key) or is_timedelta64_dtype(key): raise TypeError
Output of pd.show_versions()
INSTALLED VERSIONS ------------------ commit: None python: 3.6.0.final.0 python-bits: 64 OS: Linux OS-release: 4.4.0-53-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_CA.UTF-8
pandas: 0.18.1
nose: None
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.11.2
scipy: 0.18.1
statsmodels: None
xarray: None
IPython: 5.1.0
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: None
tables: 3.3.0
numexpr: 2.6.1
matplotlib: 1.5.3
openpyxl: None
xlrd: 1.0.0
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.5.3
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.1.4
pymysql: None
psycopg2: 2.6.2 (dt dec pq3 ext lo64)
jinja2: None
boto: None
pandas_datareader: None