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: DataFrame.sort_index is not accounting for ignore_index=True for some cases #43591

Closed
2 of 3 tasks
galipremsagar opened this issue Sep 15, 2021 · 2 comments · Fixed by #44065
Closed
2 of 3 tasks
Assignees
Labels
Bug Index Related to the Index class or subclasses Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@galipremsagar
Copy link

  • 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 master branch of pandas.

Reproducible Example

>>> import pandas as pd
>>> df = pd.DataFrame({'a': [1, 2, 3]})
>>> df.index = pd.RangeIndex(4, -1, -2)
>>> df
   a
4  1
2  2
0  3
>>> df.sort_index()
   a
0  3
2  2
4  1
>>> df.sort_index().index
Int64Index([0, 2, 4], dtype='int64')
>>> df.sort_index(ignore_index=True).index
RangeIndex(start=0, stop=3, step=1) #Index is being ignored.
>>> df.sort_index(ascending=False)
   a
4  1
2  2
0  3
>>> df.sort_index(ascending=False, ignore_index=True)
   a
4  1
2  2
0  3
>>> df.sort_index(ascending=False, ignore_index=True).index
RangeIndex(start=4, stop=-1, step=-2) # Index is not being ignored.

Issue Description

In DataFrame.sort_index, looks like ignore_index=True is being adhered to only when there is a sorting taking place. This seems to be unexpected.

Expected Behavior

The expected behavior for ignore_index=True is to always ignore the index irrespective of if there was a sorting performed or not.

Installed Versions

pd.show_versions()

INSTALLED VERSIONS

commit : 73c6825
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-76-generic
Version : #86-Ubuntu SMP Fri Jan 17 17:24:28 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.3
numpy : 1.21.2
pytz : 2021.1
dateutil : 2.8.2
pip : 21.2.4
setuptools : 58.0.4
Cython : 0.29.24
pytest : 6.2.5
hypothesis : 6.21.3
sphinx : 4.2.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.27.0
pandas_datareader: None
bs4 : 4.10.0
bottleneck : None
fsspec : 2021.08.1
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 5.0.0
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : 0.53.1

@galipremsagar galipremsagar added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 15, 2021
@mzeitlin11
Copy link
Member

Thanks for reporting this @galipremsagar! Confirmed on master, looks like ignore_index is just ignored if the sort is a noop

pandas/pandas/core/generic.py

Lines 4578 to 4582 in 72a1090

if indexer is None:
if inplace:
return
else:
return self.copy()

Investigations to fix very welcome

@mzeitlin11 mzeitlin11 added Index Related to the Index class or subclasses and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 17, 2021
@mzeitlin11 mzeitlin11 added this to the Contributions Welcome milestone Sep 17, 2021
@saehuihwang
Copy link
Contributor

take

@jreback jreback modified the milestones: Contributions Welcome, 1.4 Oct 17, 2021
@jreback jreback added the Reshaping Concat, Merge/Join, Stack/Unstack, Explode label Oct 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Index Related to the Index class or subclasses Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants