Skip to content

BUG: MultiIndex block assignment introduces NaNs in data #40186

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

Open
3 tasks done
fatvlady opened this issue Mar 2, 2021 · 5 comments
Open
3 tasks done

BUG: MultiIndex block assignment introduces NaNs in data #40186

fatvlady opened this issue Mar 2, 2021 · 5 comments
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex

Comments

@fatvlady
Copy link

fatvlady commented Mar 2, 2021

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample

import pandas as pd
import numpy as np

cols = pd.MultiIndex.from_product([[1], ['a', 'b']])
index = np.arange(2)
data = np.ones((len(index), len(cols)))
df = pd.DataFrame(data=data, index=index, columns=cols)
print(df)
df.loc[:, 1] = df.loc[:, 1]
print(df)

Problem description

When assigning to block of MultiIndex-ed dataframe (I tested with columnar multi-index), data becomes covered with NaNs instead. Same happens when doing right-assign arithmetic operations (*=, += etc.). It is not required for RHS to point to same data storage - any dataframe with corresponding index/columns gives same effect.

Expected Output

     1
     a    b
0  1.0  1.0
1  1.0  1.0
     1
     a    b
0  1.0  1.0
1  1.0  1.0

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f2c8480
python : 3.8.2.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.18362
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1251

pandas : 1.2.3
numpy : 1.18.1
pytz : 2020.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 41.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 : 2.11.1
IPython : 7.13.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.2.0
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
numba : None

@fatvlady fatvlady added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 2, 2021
@phofl phofl added Indexing Related to indexing on series/frames, not to indexes themselves and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 2, 2021
@alexprincel
Copy link
Contributor

Confirmed this happens on master branch as well.

@phofl
Copy link
Member

phofl commented Mar 4, 2021

I think df.loc[:, 1] reduces the MuldiIndex column header in dimensionality, so we can't match this?

@alexprincel
Copy link
Contributor

I am making very slow progress on understanding this but, interestingly, I just found out that indexing the dataframe instead of using loc does not have any of those issues (e.g. I can perform the operation in your code above with expected result, as well as, say, multiply by two a subset of multi-leveled columns).

See a working example :

import pandas as pd
import numpy as np

cols = pd.MultiIndex.from_product([[1], ['a', 'b']])
index = np.arange(2)
data = np.ones((len(index), len(cols)))
df = pd.DataFrame(data=data, index=index, columns=cols)
print(df)
df[1] = df[1]
print(df)

@bashtage
Copy link
Contributor

bashtage commented Jul 15, 2021

I have a proof of concept

import pandas as pd
mi = pd.MultiIndex.from_product([["a","b","c"],[1,2,3],["z","y","x"]])
df = pd.DataFrame(index=mi,dtype=float)
mi2 = pd.MultiIndex.from_product([["a","b","c"],[1,2,3]])
s = pd.Series(index=mi2, dtype=float)
s.iloc[:]=3.14
df["new"] = s
df

On 1.2.5 and 1.3.0 we get

        new
a 1 z  3.14
    y  3.14
    x  3.14
  2 z  3.14
    y  3.14
    x  3.14
  3 z  3.14
    y  3.14
    x  3.14
   ...

On master we get

       new
a 1 z  NaN
    y  NaN
    x  NaN
  2 z  NaN
    y  NaN
    x  NaN
  3 z  NaN
    y  NaN
    x  NaN

This bug is causing failures on statsmodels when testing against pandas master.

@mesvam
Copy link

mesvam commented Nov 21, 2024

This is the same as underlying issue as #45751 right?

here's another example

df = pd.DataFrame(
    np.arange(3*4).reshape((4, 3)),
    index=pd.MultiIndex.from_product([['a', 'b'], ['c', 'd']]),
    columns=['A', 'B', 'C']
)
print(df)
df.loc['a', 'A'] = df.loc['b', 'B']
print(df)
     A   B   C
a c  0   1   2
  d  3   4   5
b c  6   7   8
  d  9  10  11
       A   B   C
a c  NaN   1   2
  d  NaN   4   5
b c  6.0   7   8
  d  9.0  10  11

very unintuitive behavior

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

5 participants