BUG: locset with Series as column key fails inconsistently · Issue #59933 · pandas-dev/pandas (original) (raw)
import pandas as pd
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=list("ABC"))
df.loc[:, pd.Series(['A', 'C'])] = pd.Series([10, 20, 30]) # succeeds
df A B C 0 10 2 20 1 10 5 20
df.loc[:, pd.Series(['A', 'B', 'C'])] = pd.Series([10, 20, 30]) # fails
ValueError Traceback (most recent call last) File ~/.miniconda3/envs/snowpark/lib/python3.10/site-packages/pandas/core/internals/blocks.py:1429, in Block.setitem(self, indexer, value, using_cow) 1428 try: -> 1429 values[indexer] = casted 1430 except (TypeError, ValueError) as err:
ValueError: shape mismatch: value array of shape (2,) could not be broadcast to indexing result of shape (2,3)
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last) Cell In[5], line 1 ----> 1 df.loc[:, pd.Series(['A', 'B', 'C'])] = pd.Series([10, 20, 30])
File ~/.miniconda3/envs/snowpark/lib/python3.10/site-packages/pandas/core/indexing.py:911, in _LocationIndexer.setitem(self, key, value) 908 self._has_valid_setitem_indexer(key) 910 iloc = self if self.name == "iloc" else self.obj.iloc --> 911 iloc._setitem_with_indexer(indexer, value, self.name)
File ~/.miniconda3/envs/snowpark/lib/python3.10/site-packages/pandas/core/indexing.py:1944, in _iLocIndexer._setitem_with_indexer(self, indexer, value, name) 1942 self._setitem_with_indexer_split_path(indexer, value, name) 1943 else: -> 1944 self._setitem_single_block(indexer, value, name)
File ~/.miniconda3/envs/snowpark/lib/python3.10/site-packages/pandas/core/indexing.py:2218, in _iLocIndexer._setitem_single_block(self, indexer, value, name) 2215 self.obj._check_is_chained_assignment_possible() 2217 # actually do the set -> 2218 self.obj._mgr = self.obj._mgr.setitem(indexer=indexer, value=value) 2219 self.obj._maybe_update_cacher(clear=True, inplace=True)
File ~/.miniconda3/envs/snowpark/lib/python3.10/site-packages/pandas/core/internals/managers.py:415, in BaseBlockManager.setitem(self, indexer, value, warn) 411 # No need to split if we either set all columns or on a single block 412 # manager 413 self = self.copy() --> 415 return self.apply("setitem", indexer=indexer, value=value)
File ~/.miniconda3/envs/snowpark/lib/python3.10/site-packages/pandas/core/internals/managers.py:363, in BaseBlockManager.apply(self, f, align_keys, **kwargs) 361 applied = b.apply(f, **kwargs) 362 else: --> 363 applied = getattr(b, f)(**kwargs) 364 result_blocks = extend_blocks(applied, result_blocks) 366 out = type(self).from_blocks(result_blocks, self.axes)
File ~/.miniconda3/envs/snowpark/lib/python3.10/site-packages/pandas/core/internals/blocks.py:1432, in Block.setitem(self, indexer, value, using_cow) 1430 except (TypeError, ValueError) as err: 1431 if is_list_like(casted): -> 1432 raise ValueError( 1433 "setting an array element with a sequence." 1434 ) from err 1435 raise 1436 return self
ValueError: setting an array element with a sequence.
In [7]: df.loc[:, pd.Series(['A'])] = pd.Series([10, 20, 30]) # Fails
ValueError Traceback (most recent call last) File ~/.miniconda3/envs/snowpark/lib/python3.10/site-packages/pandas/core/internals/blocks.py:1429, in Block.setitem(self, indexer, value, using_cow) 1428 try: -> 1429 values[indexer] = casted 1430 except (TypeError, ValueError) as err:
ValueError: shape mismatch: value array of shape (2,) could not be broadcast to indexing result of shape (2,1)
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last) Cell In[7], line 1 ----> 1 df.loc[:, pd.Series(['A'])] = pd.Series([10, 20, 30])
File ~/.miniconda3/envs/snowpark/lib/python3.10/site-packages/pandas/core/indexing.py:911, in _LocationIndexer.setitem(self, key, value) 908 self._has_valid_setitem_indexer(key) 910 iloc = self if self.name == "iloc" else self.obj.iloc --> 911 iloc._setitem_with_indexer(indexer, value, self.name)
File ~/.miniconda3/envs/snowpark/lib/python3.10/site-packages/pandas/core/indexing.py:1944, in _iLocIndexer._setitem_with_indexer(self, indexer, value, name) 1942 self._setitem_with_indexer_split_path(indexer, value, name) 1943 else: -> 1944 self._setitem_single_block(indexer, value, name)
File ~/.miniconda3/envs/snowpark/lib/python3.10/site-packages/pandas/core/indexing.py:2218, in _iLocIndexer._setitem_single_block(self, indexer, value, name) 2215 self.obj._check_is_chained_assignment_possible() 2217 # actually do the set -> 2218 self.obj._mgr = self.obj._mgr.setitem(indexer=indexer, value=value) 2219 self.obj._maybe_update_cacher(clear=True, inplace=True)
File ~/.miniconda3/envs/snowpark/lib/python3.10/site-packages/pandas/core/internals/managers.py:415, in BaseBlockManager.setitem(self, indexer, value, warn) 411 # No need to split if we either set all columns or on a single block 412 # manager 413 self = self.copy() --> 415 return self.apply("setitem", indexer=indexer, value=value)
File ~/.miniconda3/envs/snowpark/lib/python3.10/site-packages/pandas/core/internals/managers.py:363, in BaseBlockManager.apply(self, f, align_keys, **kwargs) 361 applied = b.apply(f, **kwargs) 362 else: --> 363 applied = getattr(b, f)(**kwargs) 364 result_blocks = extend_blocks(applied, result_blocks) 366 out = type(self).from_blocks(result_blocks, self.axes)
File ~/.miniconda3/envs/snowpark/lib/python3.10/site-packages/pandas/core/internals/blocks.py:1432, in Block.setitem(self, indexer, value, using_cow) 1430 except (TypeError, ValueError) as err: 1431 if is_list_like(casted): -> 1432 raise ValueError( 1433 "setting an array element with a sequence." 1434 ) from err 1435 raise 1436 return self
ValueError: setting an array element with a sequence
df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], columns=list("ABC"))
df.loc[:, pd.Series(['A', 'B', 'C'])] = pd.Series([10, 20, 30]) # Succeeds
df A B C 0 10 20 30 1 10 20 30 2 10 20 30
It seems that we can only provide as many column indexers in the Series key for the column as are rows, but all of the above examples should succeed.
Columns that are indexed should be set with the corresponding value (determined by position).