Skip to content

BUG: na_position ignored when sorting MultiIndex with level!=None #51612

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
3 tasks done
gszep opened this issue Feb 24, 2023 · 4 comments · Fixed by #51672
Closed
3 tasks done

BUG: na_position ignored when sorting MultiIndex with level!=None #51612

gszep opened this issue Feb 24, 2023 · 4 comments · Fixed by #51672

Comments

@gszep
Copy link

gszep commented Feb 24, 2023

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
df = pd.DataFrame([1,2],index=pd.MultiIndex.from_tuples([(1,1),(1,pd.NA)]))

df.sort_index()
#        0
# 1 1    1
#   NaN  2

df.sort_index(level=[0,1])
#        0
# 1 NaN  2
#   1    1

# na_position seems to be ignored
df.sort_index(level=[0,1],na_position="last")
#        0
# 1 NaN  2
#   1    1

Issue Description

Sorting should be consistent whether or not all levels in level kwarg are provided. It appears that na_position is ignored when providing level.

Expected Behavior

na_position="last" should be the default behaviour no matter whether level is provided or not

Installed Versions

INSTALLED VERSIONS ------------------ commit : b69bd07 python : 3.10.4.final.0 python-bits : 64 OS : Linux OS-release : 6.0.12-76060006-generic Version : #202212290932~1674139725~22.04~ca93ccf SMP PREEMPT_DYNAMIC Thu J machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 2.1.0.dev0+47.gb69bd0755d
numpy : 1.23.5
pytz : 2022.7.1
dateutil : 2.8.2
setuptools : 65.6.3
pip : 23.0
Cython : None
pytest : 7.2.0
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.8.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : 2022.11.0
gcsfs : None
matplotlib : 3.6.2
numba : 0.56.4
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : 2022.11.0
scipy : 1.9.3
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : None
qtpy : None
pyqt5 : None

@gszep gszep added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 24, 2023
@dannyi96
Copy link
Contributor

seems like this is the expected behavior as of now.
As per docs - https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sort_index.html

na_position{‘first’, ‘last’}, default ‘last’
Puts NaNs at the beginning if first; last puts NaNs at the end. Not implemented for MultiIndex.

Could be a possible enhancement though I guess.

@phofl
Copy link
Member

phofl commented Feb 25, 2023

Yeah, can you rephrase as enhancement request?

@AlexKirko
Copy link
Member

At the very least, it would be nice to give the user a warning in this case, as there is little reason for them to expect the argument being ignored.

@phofl
Copy link
Member

phofl commented Feb 26, 2023

I'd prefer an implementation. Alternatively I'd rather raise than warn

@phofl phofl added MultiIndex and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants