Pandas version checks
-
[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.
-
[x] 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
Comment From: dannyi96
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.
Comment From: phofl
Yeah, can you rephrase as enhancement request?
Comment From: AlexKirko
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.
Comment From: phofl
I'd prefer an implementation. Alternatively I'd rather raise than warn