BUG: MultiIndex assignment via loc · Issue #35878 · pandas-dev/pandas (original) (raw)


The behavior of assignment via loc seems to have changed as of 1.0.0.

import pandas

TS = pandas.Timestamp

s = pandas.DataFrame([ ["A1", TS("2020-06-15"), -573.75], ["A2", TS("2020-06-15"), -198.75], ["A3", TS("2020-06-15"), -1.25], ], columns=["sid", "date", "price"]).set_index(["sid", "date"])["price"] s.loc[[]] = 0 print(s)

sid  date      
A1   2020-06-15    0.0
A2   2020-06-15    0.0
A3   2020-06-15    0.0
Name: price, dtype: float64

I would expect this to behave the same as in in 0.25.3 which would assign nothing. I assume this is a bug because I can't find any discussion of the changed behavior in the Changelog.

Note that selection or assignment without a MultiIndex behavior appears to be unchanged, i.e.

s = pandas.DataFrame([ ["A1", -573.75], ["A2", -198.75], ["A3", -1.25], ], columns=["sid", "price"]).set_index(["sid"])["price"] s.loc[[]] = 0 print(s)

sid
A1   -573.75
A2   -198.75
A3     -1.25
Name: price, dtype: float64

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 068e654
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-42-generic
Version : #46~18.04.1-Ubuntu SMP Fri Jul 10 07:21:24 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.0.dev0+141.g068e6542b
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.2
setuptools : 49.6.0.post20200814
Cython : 0.29.21
pytest : 6.0.1
hypothesis : 5.27.0
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.17.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None