Skip to content

BUG: multi-index df fillna with value not working after 1.4.3 #47649

Closed
@Yikun

Description

@Yikun

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

import pandas as pd
import numpy as np
pdf = pd.DataFrame(
    {
        ("x", "a"): [np.nan, 2, 3, 4, np.nan, 6],
        ("x", "b"): [1, 2, np.nan, 4, np.nan, np.nan],
        ("y", "c"): [1, 2, 3, 4, np.nan, np.nan],
    },
    index=np.random.rand(6),
)

>>> pdf.fillna({"x": -1})
           x        y
           a   b    c
0.563210 NaN NaN  1.0
0.612561 NaN NaN  2.0
0.141892 NaN NaN  3.0
0.851834 NaN NaN  4.0
0.702158 NaN NaN  NaN
0.142645 NaN NaN  NaN
>>> pdf.fillna({"x": -1, ("x", "b"): -2})
           x         y
           a    b    c
0.563210 NaN -2.0  1.0
0.612561 NaN -2.0  2.0
0.141892 NaN -2.0  3.0
0.851834 NaN -2.0  4.0
0.702158 NaN -2.0  NaN
0.142645 NaN -2.0  NaN
>>> pdf.fillna({("x", "b"): -2, "x": -1})
           x        y
           a   b    c
0.563210 NaN NaN  1.0
0.612561 NaN NaN  2.0
0.141892 NaN NaN  3.0
0.851834 NaN NaN  4.0
0.702158 NaN NaN  NaN
0.142645 NaN NaN  NaN

Issue Description

multi-index df fillna with value not working after 1.4.3

Expected Behavior

# With pandas 1.4.2:
>>> pdf.fillna({"x": -1})
            x         y
            a    b    c
0.572182 -1.0  1.0  1.0
0.819731  2.0  2.0  2.0
0.202910  3.0 -1.0  3.0
0.371307  4.0  4.0  4.0
0.038513 -1.0 -1.0  NaN
0.025520  6.0 -1.0  NaN
>>> pdf.fillna({"x": -1, ("x", "b"): -2})
            x         y
            a    b    c
0.572182 -1.0  1.0  1.0
0.819731  2.0  2.0  2.0
0.202910  3.0 -1.0  3.0
0.371307  4.0  4.0  4.0
0.038513 -1.0 -1.0  NaN
0.025520  6.0 -1.0  NaN
>>> pdf.fillna({("x", "b"): -2, "x": -1})
            x         y
            a    b    c
0.572182 -1.0  1.0  1.0
0.819731  2.0  2.0  2.0
0.202910  3.0 -2.0  3.0
0.371307  4.0  4.0  4.0
0.038513 -1.0 -2.0  NaN
0.025520  6.0 -2.0  NaN

Installed Versions

pd.show_versions()
/Users/yikun/venv/lib/python3.9/site-packages/_distutils_hack/init.py:30: UserWarning: Setuptools is replacing distutils.
warnings.warn("Setuptools is replacing distutils.")

INSTALLED VERSIONS

commit : e8093ba
python : 3.9.12.final.0
python-bits : 64
OS : Darwin
OS-release : 21.5.0
Version : Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:37 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : zh_CN.UTF-8
LOCALE : zh_CN.UTF-8

pandas : 1.4.3
numpy : 1.22.4
pytz : 2021.3
dateutil : 2.8.2
setuptools : 60.5.0
pip : 22.0.4
Cython : 0.29.28
pytest : 7.1.0
hypothesis : None
sphinx : 3.0.4
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 8.1.1
pandas_datareader: None
bs4 : 4.10.0
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : 2.0.1
matplotlib : 3.2.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 7.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.0
snappy : None
sqlalchemy : 1.4.32
tables : None
tabulate : 0.8.9
xarray : None
xlrd : None
xlwt : None
zstandard : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolateMultiIndexRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions