Description
Pandas version checks
-
I have checked that this issue has not already been reported.
-
I have confirmed this bug exists on the latest version of pandas.
-
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
>>> 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
Issue Description
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.
Expected Behavior
Columns that are indexed should be set with the corresponding value (determined by position).
Installed Versions
INSTALLED VERSIONS
commit : bdc79c1
python : 3.10.14.final.0
python-bits : 64
OS : Darwin
OS-release : 23.6.0
Version : Darwin Kernel Version 23.6.0: Mon Jul 29 21:13:04 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T6020
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.2.1
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 72.1.0
pip : 24.2
Cython : None
pytest : 7.4.4
hypothesis : None
sphinx : 5.0.2
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.4
IPython : 8.27.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.6.1
gcsfs : None
matplotlib : 3.9.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
pyarrow : 17.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.14.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None