[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()
Comment From: jreback
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.
Comment From: jreback
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