Effect of assigning list to DataFrame cell depends on unrelated column's type #25806
Labels
Bug
Indexing
Related to indexing on series/frames, not to indexes themselves
Nested Data
Data where the values are collections (lists, sets, dicts, objects, etc.).
Code Sample, a copy-pastable example if possible
Problem description
Output is:
Behavior of the operation
df.loc['a', 'foo'] = ['123']
depends on the type of thebar
column, even though thebar
column has nothing to do with the operation. If one writes a function using such operations, and a new column containing some unrelated data is added to the data frame, the function will break. The dependency on unrelated columns is also surprising.If
bar
is of typeobject
, then the list['123']
is assigned to the cell. Ifbar
is of typefloat
, then the string'123'
is assigned to the cell. If the list is longer than one element, then in the latter case one gets the exceptionValueError: Must have equal len keys and value when setting with an iterable
.Instead of
loc
, usingat
works consistently for the first two examples. However, if one changes the index's type tocategory
instead ofobject
as in the last two examples, then one observes the same inconsistency as withloc
. This is even more surprising, since one gets the sameValueError: Must have equal len keys and value when setting with an iterable
exception for longer lists, andat
is supposed to do single element assignment, but that exception suggests that it is trying to do multiple element assignment instead.Expected Output
Output of
pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.6.6.final.0
python-bits: 64
OS: Darwin
OS-release: 17.3.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
pandas: 0.24.2
pytest: None
pip: 18.1
setuptools: 39.0.1
Cython: 0.28.5
numpy: 1.15.2
scipy: 1.1.0
pyarrow: None
xarray: 0.10.9
IPython: 6.5.0
sphinx: 1.8.1
patsy: 0.5.0
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.3
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: None
html5lib: 1.0.1
sqlalchemy: 1.2.11
pymysql: None
psycopg2: 2.7.5 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: None
fastparquet: 0.1.6
pandas_gbq: None
pandas_datareader: None
gcsfs: None
The text was updated successfully, but these errors were encountered: