Skip to content

Multilevel Column Index Assignment Doesn't Work #27776

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

Closed
flyjuice opened this issue Aug 6, 2019 · 2 comments
Closed

Multilevel Column Index Assignment Doesn't Work #27776

flyjuice opened this issue Aug 6, 2019 · 2 comments
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex

Comments

@flyjuice
Copy link

flyjuice commented Aug 6, 2019

import pandas as pd

pd.set_option('display.max_columns', None)
idx = pd.IndexSlice

df = pd.DataFrame(index=pd.Index([pd.Timestamp('20080101 23:00'),
    pd.Timestamp('20080101 23:01')]), columns = pd.MultiIndex.from_product([[1,2],['A','B']]))
print(df)
df.loc[:,idx[:,'A']]=0
print(df)
df.loc[:,idx[:,'B']] = df.loc[:,idx[:,'A']]
print(df)
                       1         2     
                       A    B    A    B
2008-01-01 23:00:00  NaN  NaN  NaN  NaN
2008-01-01 23:01:00  NaN  NaN  NaN  NaN
                     1       2     
                     A    B  A    B
2008-01-01 23:00:00  0  NaN  0  NaN
2008-01-01 23:01:00  0  NaN  0  NaN
                     1       2     
                     A    B  A    B
2008-01-01 23:00:00  0  NaN  0  NaN
2008-01-01 23:01:00  0  NaN  0  NaN

I believe I use the correct Pandas idioms. But Column B is still NaN after assignment. If you initialize column B first, it also destroys the values with NaNs afterwards.

Sorry for the formatting, for some reason forum will not do what it is supposed to do for code blocks.

It may be related to but not the same:

#10440

@otro-mas1
Copy link

otro-mas1 commented Aug 9, 2019

Only need add .values like in #10440

df.loc[:,idx[:,'B']] = df.loc[:,idx[:,'A']].values
`

@mroeschke mroeschke added Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex labels Nov 2, 2019
@phofl
Copy link
Member

phofl commented Apr 18, 2023

This works as expected, closing for now since we have a bunch of open issues about this

@phofl phofl closed this as completed Apr 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Projects
None yet
Development

No branches or pull requests

4 participants