Skip to content

BUG: Passing level= to DataFrame.reindex fails when the index is flat #35132

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

Closed
1 task
zhenruiliao opened this issue Jul 6, 2020 · 1 comment · Fixed by #46327
Closed
1 task

BUG: Passing level= to DataFrame.reindex fails when the index is flat #35132

zhenruiliao opened this issue Jul 6, 2020 · 1 comment · Fixed by #46327
Labels
API - Consistency Internal Consistency of API/Behavior Bug MultiIndex
Milestone

Comments

@zhenruiliao
Copy link

zhenruiliao commented Jul 6, 2020

  • [ X] I have checked that this issue has not already been reported.

  • [ X] 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.


Code Sample, a copy-pastable example

import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.randn(100,40), index=pd.Index(np.arange(100), name='foo'))
new_index = pd.Index(np.arange(50), name='foo')
df.reindex(new_index, level='foo')

Problem description

Currently the above raises AttributeError: 'Int64Index' object has no attribute 'levels'.

The behavior of DataFrame.reindex should be consistent between flat Indexes and MultiIndexes - as long the level is valid, I shouldn't have to check whether the index is a flat index or a MultiIndex before deciding whether to pass level=.

Expected Output

Same as df.reindex(new_index)

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.3.0-53-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.5
numpy : 1.18.4
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 47.3.1.post20200622
Cython : 0.29.19
pytest : None
hypothesis : None
sphinx : 3.1.1
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.4.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.15.0
pandas_datareader: None
bs4 : 4.9.0
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.4.1
matplotlib : 3.1.3
numexpr : 2.7.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : 3.4.4
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : None
numba : 0.48.0

@zhenruiliao zhenruiliao added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 6, 2020
@TomAugspurger
Copy link
Contributor

Hmm, agreed that this should probably work, but we'll need to think through all the ramifications. Can you work on this a bit @zhenruiliao and see what comes up?

@TomAugspurger TomAugspurger added API - Consistency Internal Consistency of API/Behavior MultiIndex and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 4, 2020
@jreback jreback added this to the 1.5 milestone Mar 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API - Consistency Internal Consistency of API/Behavior Bug MultiIndex
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants