Skip to content

BUG: ValueError when assigning list in a single-row object dataframe  #37486

Closed
@paxcema

Description

@paxcema

Code Sample, a copy-pastable example

import pandas as pd

df = pd.DataFrame([['a', '1', '2.0']], columns=['a', 'b', 'c'], dtype=object)
df['c'].iloc[0] = [df['c'].iloc[0]]         

Problem description

The current behaviour raises a ValueError, even though the assignment happened. Stack trace:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-12824bd17b66> in <module>()
      2 
      3 df = pd.DataFrame([['a', '1', '2.0']], columns=['a', 'b', 'c'], dtype=object)
----> 4 df['c'].iloc[0] = [df['c'].iloc[0]]

4 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py in __setitem__(self, key, value)
    668 
    669         iloc = self if self.name == "iloc" else self.obj.iloc
--> 670         iloc._setitem_with_indexer(indexer, value)
    671 
    672     def _validate_key(self, key, axis: int):

/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py in _setitem_with_indexer(self, indexer, value)
   1800             # actually do the set
   1801             self.obj._consolidate_inplace()
-> 1802             self.obj._mgr = self.obj._mgr.setitem(indexer=indexer, value=value)
   1803             self.obj._maybe_update_cacher(clear=True)
   1804 

/usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py in setitem(self, indexer, value)
    532 
    533     def setitem(self, indexer, value) -> "BlockManager":
--> 534         return self.apply("setitem", indexer=indexer, value=value)
    535 
    536     def putmask(

/usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py in apply(self, f, align_keys, **kwargs)
    404                 applied = b.apply(f, **kwargs)
    405             else:
--> 406                 applied = getattr(b, f)(**kwargs)
    407             result_blocks = _extend_blocks(applied, result_blocks)
    408 

/usr/local/lib/python3.6/dist-packages/pandas/core/internals/blocks.py in setitem(self, indexer, value)
    885             values[indexer] = value
    886 
--> 887             values = values.astype(arr_value.dtype, copy=False)
    888 
    889         # set

ValueError: setting an array element with a sequence

Expected Output

As the dtype is object, I would have expected this assignment to happen without raising a ValueError. If the dataframe has more than one row:

df = pd.DataFrame([['a', '1', '2.0']]*2, columns=['a', 'b', 'c'])
df['c'].iloc[0] = [df['c'].iloc[0]]

Then the exception is not thrown.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : db08276
python : 3.6.9.final.0
python-bits : 64
OS : Linux
OS-release : 4.19.112+
Version : #1 SMP Thu Jul 23 08:00:38 PDT 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.3
numpy : 1.18.5
pytz : 2018.9
dateutil : 2.8.1
pip : 19.3.1
setuptools : 50.3.0
Cython : 0.29.21
pytest : 3.6.4
hypothesis : None
sphinx : 1.8.5
blosc : None
feather : 0.4.1
xlsxwriter : None
lxml.etree : 4.2.6
html5lib : 1.0.1
pymysql : None
psycopg2 : 2.7.6.1 (dt dec pq3 ext lo64)
jinja2 : 2.11.2
IPython : 5.5.0
pandas_datareader: 0.9.0
bs4 : 4.6.3
bottleneck : 1.3.2
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.2.2
numexpr : 2.7.1
odfpy : None
openpyxl : 2.5.9
pandas_gbq : 0.13.3
pyarrow : 0.14.1
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.20
tables : 3.4.4
tabulate : 0.8.7
xarray : 0.15.1
xlrd : 1.1.0
xlwt : 1.3.0
numba : 0.48.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions