Skip to content
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

BUG: loc/iloc insertion inserts array, scalar expected #19590

Closed
topper-123 opened this issue Feb 8, 2018 · 2 comments
Closed

BUG: loc/iloc insertion inserts array, scalar expected #19590

topper-123 opened this issue Feb 8, 2018 · 2 comments
Labels
Bug Duplicate Report Duplicate issue or pull request Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@topper-123
Copy link
Contributor

topper-123 commented Feb 8, 2018

[1] df = pd.DataFrame([[1,2]])
[2] df.iloc[0, 1:] = df.iloc[0, 1:].apply('{:+.2f}'.format)
[3] df
   0       1
0  1  [+2.00]

Notice that an array was inserted, where a scalar was expected. The issue is present in both 0.22 and master.

The same issue is if we use loc instead of iloc. However, if we insert a numeric series instead of a string series, everything is ok:

[1] df = pd.DataFrame([[1,2]])
[2] df.iloc[0, 1:] = df.iloc[0, 1:].apply(lambda x: x*2)
[3] df
   0       2
0  1      4 

EDIT: the issue can be simplified further:

[1] df = pd.DataFrame([[1, 2]])
[2] df.iloc[0, 1:] = ['a']
[3] df
   0       2
0  1      [a]
[4] df = pd.DataFrame([[1, 2]])
[5] df.iloc[0, 0:] = ['a', 'b']  # ok
   0  1
0  a  b
[6] df = pd.DataFrame([[1, 2, 3]])
[7] df.iloc[0, 1:] = ['a', 'b']  # ok
   0  1  2
0  1  a  b

So it seems like a very specific bug, concerning insertion into the last column of a dataframe.

Expected Output

Expected is that a scalar value is inserted into the data frame, and not an array.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: 49714e8
python: 3.6.3.final.0
python-bits: 32
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.22.0.dev0+669.g49714e8
pytest: 3.3.1
pip: 9.0.1
setuptools: 38.2.5
Cython: 0.26.1
numpy: 1.13.3
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: 1.6.3
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.1.0
openpyxl: 2.4.9
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0b10
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@jreback
Copy link
Contributor

jreback commented Feb 8, 2018

xref #14592
xref #19474
xref #16864

I'll mark it though the above are all tied up in this. Assigning a list requires inference wether it should resolve to an error (if mismatching the length), or be broadcast

a PR to investigate / fix would be welcome.

@jreback jreback added Bug Indexing Related to indexing on series/frames, not to indexes themselves labels Feb 8, 2018
@jreback jreback added this to the Next Major Release milestone Feb 8, 2018
@jreback
Copy link
Contributor

jreback commented Feb 8, 2018

actually on reflection, you are assigning to a single list-like slot and you have a len(1) list

In [14]: df.iloc[0, 1:] = ['a', 'b']
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-14-ddcf638d1f6e> in <module>()
----> 1 df.iloc[0, 1:] = ['a', 'b']

~/pandas/pandas/core/indexing.py in __setitem__(self, key, value)
    185             key = com._apply_if_callable(key, self.obj)
    186         indexer = self._get_setitem_indexer(key)
--> 187         self._setitem_with_indexer(indexer, value)
    188 
    189     def _has_valid_type(self, k, axis):

~/pandas/pandas/core/indexing.py in _setitem_with_indexer(self, indexer, value)
    591 
    592                     if len(labels) != len(value):
--> 593                         raise ValueError('Must have equal len keys and value '
    594                                          'when setting with an iterable')
    595 

ValueError: Must have equal len keys and value when setting with an iterable

e.g. the above should be clear that the list must be the same length. This is almost exactly #19474, closing as a duplicate

@jreback jreback closed this as completed Feb 8, 2018
@jreback jreback added the Duplicate Report Duplicate issue or pull request label Feb 8, 2018
@jreback jreback modified the milestones: Next Major Release, No action Feb 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Duplicate Report Duplicate issue or pull request Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

No branches or pull requests

2 participants