You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The sort_index() method will sort the dataframe by the index values. If the dataframe has a multiindex, then it sorts by the indexes in order. However, if you only want to sort by a subset of the indexes and leave the others in their current order, you can use the level keyword argument to specify a level (or list of levels) to sort on, and set sort_remaining=False to ignore the other levels. The sort_remaining keyword seems to work correctly when ascending=True (default), but when passing ascending=False and sort_remaining=False, Pandas continues to sort the other indices (in fact in ascending order, too).
Note that this happens regardless if you specify levels in any of the supported formats; viz.
There have been a few issues/PRs on the sort_index() method in the past year, so I'm not sure if one of those PRs broke this ability or whether this has been around longer or is unrelated.
Expected Output
In [9]: df.sort_index(level='index_1', ascending=False, sort_remaining=False)
Out[9]:
0index_1index_2320.55801910.096064220.35317610.153776120.81218110.313342
Note that the expected outcome could be achieved in this example with
Script to reproduce
iPython example output
Problem description
Documentation for
sort_index()
: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_index.htmlThe
sort_index()
method will sort the dataframe by the index values. If the dataframe has a multiindex, then it sorts by the indexes in order. However, if you only want to sort by a subset of the indexes and leave the others in their current order, you can use thelevel
keyword argument to specify a level (or list of levels) to sort on, and setsort_remaining=False
to ignore the other levels. Thesort_remaining
keyword seems to work correctly whenascending=True
(default), but when passingascending=False
andsort_remaining=False
, Pandas continues to sort the other indices (in fact in ascending order, too).Note that this happens regardless if you specify levels in any of the supported formats; viz.
There have been a few issues/PRs on the
sort_index()
method in the past year, so I'm not sure if one of those PRs broke this ability or whether this has been around longer or is unrelated.Expected Output
Note that the expected outcome could be achieved in this example with
or similar setting/resetting of indexes. Well, or you could just do nothing since the dataframe is already in that order, but you get the point.
Output of
pd.show_versions()
pandas: 0.23.4
pytest: None
pip: 18.0
setuptools: 36.4.0
Cython: None
numpy: 1.13.1
scipy: 0.19.1
pyarrow: None
xarray: None
IPython: 6.1.0
sphinx: 1.7.4
patsy: None
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.1.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
The text was updated successfully, but these errors were encountered: