-
-
Notifications
You must be signed in to change notification settings - Fork 18k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ValueError when using loc with TimedeltaIndex #14946
Comments
this is quite subtle. When a boolean array is presented, This patch makes it work. If you could do a PR with some tests and a 1-liner about why this is needed, would be great. The basic issue is that
|
…dev#14946) closes pandas-dev#14946 Author: Matt Roeschke <emailformattr@gmail.com> Closes pandas-dev#15221 from mroeschke/fix_14946 and squashes the following commits: b8ac04e [Matt Roeschke] BUG: TimedelaIndex raising ValueError when boolean indexing (pandas-dev#14946)
This snippet will work correctly:
But when using a TimedeltaIndex:
pandas 0.18.1 has a different problem, but there is a workaround. Simple use the underlying numpy array:
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
The text was updated successfully, but these errors were encountered: