Skip to content

BUG: Using None in nested object MultiIndex causes reindex to misalign. #42883

Closed
@DriesSchaumont

Description

@DriesSchaumont
  • 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.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

>>> import pandas as pd
>>> index = pd.MultiIndex.from_tuples([(('a', None), 1), (('b', None), 2)])
>>> index2 = pd.MultiIndex.from_tuples([(('b', None), 2), (('a', None), 1)])
>>> df1_dtype = pd.DataFrame([1,2], index=index)
>>> df2_dtype = pd.DataFrame([2,1], index=index2)
>>> df1_dtype.reindex_like(df2_dtype)
              0
(b, None) 2 NaN
(a, None) 1 NaN
>>> import pandas as pd
>>> import numpy as np
>>> index = pd.MultiIndex.from_tuples([(('a', np.nan), 1), (('b', np.nan), 2)])
>>> index2 = pd.MultiIndex.from_tuples([(('b', np.nan), 2), (('a', np.nan), 1)])
>>> df1_dtype = pd.DataFrame([1,2], index=index)
>>> df2_dtype = pd.DataFrame([2,1], index=index2)
>>> df1_dtype.reindex_like(df2_dtype)
            0
(b, nan) 2  2
(a, nan) 1  1

Problem description

I would expect the two indices to align here.

Expected Output

>>> import pandas as pd
>>> index = pd.MultiIndex.from_tuples([(('a', None), 1), (('b', None), 2)])
>>> index2 = pd.MultiIndex.from_tuples([(('b', None), 2), (('a', None), 1)])
>>> df1_dtype = pd.DataFrame([1,2], index=index)
>>> df2_dtype = pd.DataFrame([2,1], index=index2)
>>> df1_dtype.reindex_like(df2_dtype)
            0
(b, None) 2  2
(a, None) 1  1

Output of pd.show_versions()

>>> pd.show_versions()

INSTALLED VERSIONS
------------------
commit           : 226876a55b82324b92449256bab47d431b22310f
python           : 3.8.5.final.0
python-bits      : 64
OS               : Linux
OS-release       : 4.19.128-microsoft-standard
Version          : #1 SMP Tue Jun 23 12:58:10 UTC 2020
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : None
LANG             : C.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.4.0.dev0+357.g226876a55b
numpy            : 1.21.1
pytz             : 2021.1
dateutil         : 2.8.2
pip              : 21.2.2
setuptools       : 57.4.0
Cython           : 0.29.24
pytest           : 6.2.4
hypothesis       : 6.14.5
sphinx           : 4.1.2
blosc            : 1.10.4
feather          : None
xlsxwriter       : 1.4.4
lxml.etree       : 4.6.3
html5lib         : 1.1
pymysql          : None
psycopg2         : None
jinja2           : 3.0.1
IPython          : 7.25.0
pandas_datareader: None
bs4              : 4.9.3
bottleneck       : 1.3.2
fsspec           : 2021.05.0
fastparquet      : 0.6.3
gcsfs            : 2021.07.0
matplotlib       : 3.4.2
numexpr          : 2.7.3
odfpy            : None
openpyxl         : 3.0.7
pandas_gbq       : None
pyarrow          : 5.0.0
pyxlsb           : None
s3fs             : None
scipy            : 1.7.0
sqlalchemy       : 1.4.22
tables           : 3.6.1
tabulate         : 0.8.9
xarray           : 0.18.2
xlrd             : 2.0.1
xlwt             : 1.3.0
numba            : 0.53.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolateMultiIndexNested DataData where the values are collections (lists, sets, dicts, objects, etc.).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions