-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: NaNs generated when mutating subset of column MultiIndex via loc #45751
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
I think this is kind of expected, but certainly weird. If you slice a MultiIndex only on one level, this level gets removed, hence your rhs has a regular index. We try aligning this with the original df, which fails, hence the nans. Investigations welcome |
@phofl Thanks for the reply. I'm still a bit confused why it works with the first example in the expected behavior (passing an array of only one element) and not in the case I marked a bug. I did my best to figure out if it was behaving as designed and I'm not trying to waste anyone's time. I'm sorry if I am. Obviously there's some reason in the code, but it is odd that
I haven't looked too deep into pandas code and work has me terribly backlogged at the moment, but if I can get caught up on work, maybe I'll look a little deeper into it. Thanks for your reply. I'm still learning pandas. |
Inplace modification is not how pandas works. We apply a getitem onto df, divide it by 2 and set it back onto the original df via setitem. the getitem reduces the MultiIndex to a regular index, because the level is dropped. when you assign a scalar, no alignment is done. I think if you replace a with (a, slice(None)) this should work |
It seems these ways can get the expected result: While quite weird. |
Another example from my notes:
ATM this warns that it will raise in the future (PDEP6) |
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 appears that
loc
does no work correctly with modification operators when I would guess that there is some "fast path" code onloc
which assumes aSeries
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:
Or, to work as the following works:
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
The text was updated successfully, but these errors were encountered: