Description
This snippet will work correctly:
import pandas as pd
df = pd.DataFrame({'x':range(10)})
cond = df['x'] > 3
df.loc[cond, 'x'] = 10 # Works properly
But when using a TimedeltaIndex:
import pandas as pd
df = pd.DataFrame({'x':range(10)})
df.index = pd.to_timedelta(range(10), unit='s')
cond = df['x'] > 3
df.loc[cond, 'x'] = 10 # ValueError is raised
pandas 0.18.1 has a different problem, but there is a workaround. Simple use the underlying numpy array:
df.loc[cond.values, 'x'] = 10 # Works as intended
df.loc[cond, 'x'] = 10 # No error, wrong results
Ouput
ValueError Traceback (most recent call last)
in ()
----> 1 df.loc[cond, 'x'] = 10
~/pandas/pandas/core/indexing.py in setitem(self, key, value)
138 else:
139 key = com._apply_if_callable(key, self.obj)
--> 140 indexer = self._get_setitem_indexer(key)
141 self._setitem_with_indexer(indexer, value)
142
~/pandas/pandas/core/indexing.py in _get_setitem_indexer(self, key)
120
121 if isinstance(key, tuple) and not self.ndim < len(key):
--> 122 return self._convert_tuple(key, is_setter=True)
123 if isinstance(key, range):
124 return self._convert_range(key, is_setter=True)
~/pandas/pandas/core/indexing.py in _convert_tuple(self, key, is_setter)
182 else:
183 for i, k in enumerate(key):
--> 184 idx = self._convert_to_indexer(k, axis=i, is_setter=is_setter)
185 keyidx.append(idx)
186 return tuple(keyidx)
~/pandas/pandas/core/indexing.py in _convert_to_indexer(self, obj, axis, is_setter)
1149 # if we are a label return me
1150 try:
-> 1151 return labels.get_loc(obj)
1152 except LookupError:
1153 if isinstance(obj, tuple) and isinstance(labels, MultiIndex):
~/pandas/pandas/tseries/tdi.py in get_loc(self, key, method, tolerance)
675 """
676
--> 677 if isnull(key):
678 key = tslib.NaT
679
~/pandas/pandas/core/generic.py in nonzero(self)
915 raise ValueError("The truth value of a {0} is ambiguous. "
916 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
--> 917 .format(self.class.name))
918
919 bool = nonzero
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Versions
Here is my pd.show_versions(). It also has been tested on windows7 64 bits with version 0.19
INSTALLED VERSIONS
commit: f79bc7a
python: 3.5.2.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-57-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_CA.UTF-8
LOCALE: en_CA.UTF-8
pandas: 0.19.0+243.gf79bc7a
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: None
numexpr: None
matplotlib: None
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
s3fs: None
pandas_datareader: None