pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
42.62k stars 17.57k forks source link

BUG: #59170

Open seanv507 opened 2 days ago

seanv507 commented 2 days ago

Pandas version checks

Reproducible Example

import pandas as pd
z3 = pd.DataFrame({"a":[10], "b":[2], "c": [1]}).set_index(["a", "b"])
maturities = list(range(100))
z4 = z3.reindex(maturities,level=1)

Issue Description

I create a 1 row dataframe with multiindex I reindex on a single level, and I expect all values in the new index to be in the new dataframe. instead, the same dataframe is returned.

the same happens with more than a single row.

If I use a 1d index, or apply to the columns, I get a 100 row/column dataframe as expected

Expected Behavior

expected_output = pd.DataFrame({"a":10, "b": range(100), "c": np.nan}).set_index(["a", "b"])
expected_output.loc[(10,2), "c"]=1

expected_output.head()
#          c
# a  b     
# 10 0  NaN
#    1  NaN
#    2  1.0
#    3  NaN
#    4  NaN

Installed Versions

INSTALLED VERSIONS ------------------ commit : e86ed377639948c64c429059127bcf5b359ab6be python : 3.11.5.final.0 python-bits : 64 OS : Darwin OS-release : 23.5.0 Version : Darwin Kernel Version 23.5.0: Wed May 1 20:19:05 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T8112 machine : arm64 processor : arm byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8 pandas : 2.1.1 numpy : 1.26.0 pytz : 2023.3.post1 dateutil : 2.8.2 setuptools : 68.2.2 pip : 23.2.1 Cython : None pytest : None hypothesis : None sphinx : 7.2.6 blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : 3.1.2 IPython : 8.15.0 pandas_datareader : None bs4 : None bottleneck : None dataframe-api-compat: None fastparquet : None fsspec : None gcsfs : None matplotlib : 3.8.0 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