-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DataFrame.loc[n] = dict(..) fails with some type combinations #16309
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
Comments
Thanks for the report. Yeah it looks like #13829. I once prepared a draft fix, and hopefully work on it again. |
@bmcfee I cannot reproduce this anymore on master or with 0.20.2 (but fails on 0.20.1), so it seems this somehow got fixed. And if so, would also be nice to add some tests to keep it working. |
Confirmed that the above example now works on 0.20.2 (conda distribution). However, I now get a different error if I try to update an existing record, even with identical contents: In [1]: import pandas as pd
In [2]: d = pd.DataFrame(columns=['time', 'value'])
In [3]: d.loc[1] = dict(time=pd.to_timedelta(6, unit='s'), value='foo')
In [4]: d.loc[1] = dict(time=pd.to_timedelta(6, unit='s'), value='foo')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-20345bf5ca35> in <module>()
----> 1 d.loc[1] = dict(time=pd.to_timedelta(6, unit='s'), value='foo')
/home/bmcfee/miniconda/envs/py35/lib/python3.5/site-packages/pandas/core/indexing.py in __setitem__(self, key, value)
177 key = com._apply_if_callable(key, self.obj)
178 indexer = self._get_setitem_indexer(key)
--> 179 self._setitem_with_indexer(indexer, value)
180
181 def _has_valid_type(self, k, axis):
/home/bmcfee/miniconda/envs/py35/lib/python3.5/site-packages/pandas/core/indexing.py in _setitem_with_indexer(self, indexer, value)
581
582 for item, v in zip(labels, value):
--> 583 setter(item, v)
584 else:
585
/home/bmcfee/miniconda/envs/py35/lib/python3.5/site-packages/pandas/core/indexing.py in setter(item, v)
511 s._consolidate_inplace()
512 s = s.copy()
--> 513 s._data = s._data.setitem(indexer=pi, value=v)
514 s._maybe_update_cacher(clear=True)
515
/home/bmcfee/miniconda/envs/py35/lib/python3.5/site-packages/pandas/core/internals.py in setitem(self, **kwargs)
3201
3202 def setitem(self, **kwargs):
-> 3203 return self.apply('setitem', **kwargs)
3204
3205 def putmask(self, **kwargs):
/home/bmcfee/miniconda/envs/py35/lib/python3.5/site-packages/pandas/core/internals.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
3089
3090 kwargs['mgr'] = self
-> 3091 applied = getattr(b, f)(**kwargs)
3092 result_blocks = _extend_blocks(applied, result_blocks)
3093
/home/bmcfee/miniconda/envs/py35/lib/python3.5/site-packages/pandas/core/internals.py in setitem(self, indexer, value, mgr)
684
685 # coerce args
--> 686 values, _, value, _ = self._try_coerce_args(self.values, value)
687 arr_value = np.array(value)
688
/home/bmcfee/miniconda/envs/py35/lib/python3.5/site-packages/pandas/core/internals.py in _try_coerce_args(self, values, other)
1754 else:
1755 # scalar
-> 1756 other = Timedelta(other)
1757 other_mask = isnull(other)
1758 other = other.value
pandas/_libs/tslib.pyx in pandas._libs.tslib.Timedelta.__new__ (pandas/_libs/tslib.c:50070)()
pandas/_libs/tslib.pyx in pandas._libs.tslib.parse_timedelta_string (pandas/_libs/tslib.c:60542)()
ValueError: unit abbreviation w/o a number I suspect this is an entirely different kind of error, so it might make sense to close this one out and start a new issue, but I'll leave that call to you. |
Ah, yes, I see that as well (so when the label already exists). This already is raising in 0.19.2, so not a new bug .. |
Works now, setting once and setting twice |
Code Sample, a copy-pastable example if possible
This one fails:
But this one succeeds:
This one also succeeds:
Problem description
[this should explain why the current behaviour is a problem and why the expected output is a better solution.]
The current behavior is a problem because it is inconsistent, and depends on the type of data provided. Mixing
timedelta
withstr
fails, buttimedelta
withint
works, as doesint
withstr
.I believe this is related to aggressive type inference previously noted in #13829.
Expected Output
Not crashing.
Output of
pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.5.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-77-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
pandas: 0.20.1
pytest: 3.0.7
pip: 9.0.1
setuptools: 35.0.2
Cython: 0.25.2
numpy: 1.12.1
scipy: 0.19.0
xarray: 0.9.5
IPython: 6.0.0
sphinx: 1.5.5
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: 1.2.0
tables: None
numexpr: 2.6.0
feather: None
matplotlib: 2.0.1
openpyxl: None
xlrd: 1.0.0
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999
sqlalchemy: 1.0.9
pymysql: None
psycopg2: None
jinja2: 2.9.5
s3fs: 0.1.0
pandas_gbq: None
pandas_datareader: None
The text was updated successfully, but these errors were encountered: