Description
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
a = pd.MultiIndex.from_arrays([["a","a"],["b","b"]])
b = pd.MultiIndex.from_arrays([["a"]])
df = pd.DataFrame(index=a)
s = pd.Series([0.4],index=b)
df["new_col"]=s
Issue Description
When inserting a Series with a smaller MultiIndex, pandas 2.1.0 will raise
AssertionError: Length of new_levels (1) must be same as self.nlevels (2)
This was not the case with pandas < 2.1 (including 2.0.3).
In pseudo code, the existing df has an MultiIndex like
[["a", "a"],
["a", "b"]]
while the Series
to be inserted as a MultIndex like
[["a"],
["a"]]
so that it aligns on the first level. Previously this would expand.
Expected Behavior
Insertion of smaller MultiIndex into larger MultiIndex should work as it previously has.
Installed Versions
INSTALLED VERSIONS
commit : ba1cccd
python : 3.9.17.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22621
machine : AMD64
processor : AMD64 Family 23 Model 113 Stepping 0, AuthenticAMD
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252
pandas : 2.1.0
numpy : 1.25.2
pytz : 2023.3
dateutil : 2.8.2
setuptools : 68.0.0
pip : 23.2.1
Cython : 0.29.36
pytest : 7.4.0
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.14.0
pandas_datareader : None
bs4 : None
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.3
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.11.2
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None