Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] 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
df = pandas.DataFrame(index=[0, 1], columns=pandas.MultiIndex.from_tuples([("a", "a1"), ("b", "b2"), ("c", "c2")]), data=0.0)
print("initial df")
print(df)
# the first two slices leads to buggy behavior
for sl in ["a", ("b",), ("c", slice(None))]:
print()
print(f"assigning with df.loc[:, {repr(sl)}] += 1.")
print("before ", df.loc[:, sl])
df.loc[:, sl] += 1.
print("after", df.loc[:, sl])
print()
print("final df")
print(df)
Issue Description
using df.loc[:, first_level_of_mutliindex_column] += 1
assigns NaN instead of adding 1 to the existing value.
One must index the column with df.loc[:, (first_level_of_mutliindex_column, slice(None))]
to make it work.
Yet getting df.loc[:, first_level_of_mutliindex_column]
works as expected.
Expected Behavior
End up with
a b c
a1 b2 c2
0 1.0 1.0 1.0
1 1.0 1.0 1.0
instead of
a b c
a1 b2 c2
0 NaN NaN 1.0
1 NaN NaN 1.0
Installed Versions
Comment From: phofl
Hi, thanks for your report. This is a duplicate, we have open issues about it.
Comment From: sdementen
Ok sorry. I could not find a related issue. If you have it, I would be grateful but no problem if this takes too lich of your time
On Thu, 23 Mar 2023, 12:44 Patrick Hoefler, @.***> wrote:
Hi, thanks for your report. This is a duplicate, we have open issues about it.
— Reply to this email directly, view it on GitHub https://github.com/pandas-dev/pandas/issues/52134#issuecomment-1481042277, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJ6S5TQM4UGGWOXOFR3RT3W5QZRTANCNFSM6AAAAAAWE4JBNA . You are receiving this because you authored the thread.Message ID: @.***>