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
>>> x = pd.DataFrame({("a","x"): [1,2,3], ("b","x"): [4,5,6], ("b", "y"): [7,8,9]})
>>> x.loc[:, 'b'] /= 2
>>> x
a b
x x y
0 1 NaN NaN
1 2 NaN NaN
2 3 NaN NaN
Issue Description
It appears that loc
does no work correctly with modification operators when I would guess that there is some "fast path" code on loc
which assumes a Series
output if a scalar is passed for the second argument. Likely the programmer didn't consider that it may actually match multiple columns.
Expected Behavior
I would expect the the above to work the same as the following:
>>> x = pd.DataFrame({("a","x"): [1,2,3], ("b","x"): [4,5,6], ("b", "y"): [7,8,9]})
>>> x.loc[:, ['b']] /= 2
>>> x
a b
x x y
0 1 2.0 3.5
1 2 2.5 4.0
2 3 3.0 4.5
Or, to work as the following works:
>>> x = pd.DataFrame({("a","x"): [1,2,3], ("b","x"): [4,5,6], ("b", "y"): [7,8,9]})
>>> x.loc[:, 'b'] = 2
>>> x
a b
x x y
0 1 2 2
1 2 2 2
2 3 2 2
Installed Versions
INSTALLED VERSIONS
commit : bb1f651
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-94-generic
Version : #106-Ubuntu SMP Thu Jan 6 23:58:14 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.4.0
numpy : 1.22.0
pytz : 2021.3
dateutil : 2.8.2
pip : 20.0.2
setuptools : 45.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 7.30.1
pandas_datareader: None
bs4 : 4.8.2
bottleneck : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None