Skip to content
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

BUG: #59170

Open
2 of 3 tasks
seanv507 opened this issue Jul 2, 2024 · 2 comments
Open
2 of 3 tasks

BUG: #59170

seanv507 opened this issue Jul 2, 2024 · 2 comments
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@seanv507
Copy link

seanv507 commented Jul 2, 2024

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
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 : e86ed37 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

@seanv507 seanv507 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 2, 2024
@ritwizsinha
Copy link
Contributor

Thanks for submitting this bug, I suppose that this might be an enhancement as reindex doesn't have the capability to fill up index values as well after reindexing. The documentation for reindex method says for the named argument level

Broadcast across a level, matching Index values on the passed MultiIndex level.

Because it matches values, thus it discards anything which is not 1-1 mapped on the index. The fill_value is only for the values to the indexes and not the index itself.

@seanv507
Copy link
Author

Sorry, I think I misunderstood the documentation. I expected the level argument to allow me to reindex on only a single level of a multiindex, where I pass a 1D index, and expect it to create the cartesian product of remaining index columns and my passed 1D index.

So to me the "broadcasting" was replicating the specified level values across all the remaining index columns.

I think this guy had a similar misunderstanding

https://stackoverflow.com/questions/59471286/reindexing-only-level-of-a-multiindex-dataframe-reindex-broken

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

2 participants