-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: locset with Series as column key fails inconsistently #59933
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
Comments
Thanks for the report. I'm not sure this was ever meant to be supported. From the documentation: https://pandas.pydata.org/docs/dev/reference/api/pandas.DataFrame.loc.html The (relevant) supported arguments are:
While you are providing a Series, it is not a Boolean Series. However
has the same issue. It appears that the preparation in Further investigations and PRs to fix are welcome! |
I tried to investigate a bit more when i saw df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=list("ABC"))
df.iloc[:, pd.Series([0, 1, 2])] = pd.Series([10, 20, 30])
Therefore, you might want to use where this is stemming from - i see a difference in behavior between pandas/pandas/core/indexing.py Lines 2173 to 2179 in 05fa958
pandas/pandas/core/indexing.py Lines 2357 to 2371 in 05fa958
Even if you try to provide both the slices instead of just column slice to In [35]: df.loc[df.index, pd.Series(['A', 'B', 'C'])] = pd.Series([10, 20, 30])
In [36]: df
Out[36]:
A B C
0 10 10 10
1 20 20 20
|
take |
I've found that assignment behaves differently depending on indexing. However, it is not clear how exactly this should work. Case 1. Indexing with boolean Series >>> df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], columns=list("ABC"))
>>> df.loc[df['A']>0, ['A', 'B', 'C']] = pd.Series([10, 20, 30])
>>> df
A B C
0 10 10 10
1 20 20 20
2 30 30 30 Case 2. Indexing with slice (should be equivalent with boolean array, but not) >>> df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], columns=list("ABC"))
>>> df.loc[:, ['A', 'B', 'C']] = pd.Series([10, 20, 30])
>>> df
A B C
0 10 20 30
1 10 20 30
2 10 20 30 I propose to define it this way: for cases where we assign a vector (Series or list or smth else) to an array and the length of the vector is equal to the number of target columns the result should be similar to Case 2 |
Found a test that confirms the correctness of Case 1. pandas/pandas/tests/indexing/test_indexing.py Lines 475 to 492 in 05fa958
|
Comments about pull-request:
>>> df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], columns=list("ABC"))
>>> df.loc[:, ['A', 'B', 'C']] = pd.Series([10, 20, 30])
>>> df
A B C
0 10 10 10
1 20 20 20
2 30 30 30
>>> df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=list("ABC"))
>>> df.loc[:, pd.Series(['A', 'B', 'C'])] = pd.Series([10, 20, 30])
>>> df
A B C
0 10 10 10
1 20 20 20 |
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
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
The text was updated successfully, but these errors were encountered: