BUG: Index.sort_values with natsort key · Issue #56081 · pandas-dev/pandas (original) (raw)
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 import natsort pd.Index([1, 3, 2]).sort_values(key=natsort.natsort_key)
Issue Description
Natural sorting works as expected with Series
:
pd.Series([1, 3, 2]).sort_values(key=natsort.natsort_key)
Expecting that Index
behaves the same, but it raises an exception:
pd.Index([1, 3, 2]).sort_values(key=natsort.natsort_key)
TypeError: nargsort does not support MultiIndex. Use index.sort_values instead.
Expected Behavior
Expecting that key=natsort_key
works the same in Index.sort_values
and Series.sort_values
.
As a workaround, this works, but seems unnecessarily verbose:
pd.Index([1, 3, 2]).sort_values(key=lambda v: pd.Index(natsort.natsort_key(v), tupleize_cols=False))
Installed Versions
INSTALLED VERSIONS
commit : 9aa176687c04227af1c645712ea73296861858a3 python : 3.10.13.final.0 python-bits : 64 OS : Linux OS-release : 6.6.1-arch1-1 Version : #1 SMP PREEMPT_DYNAMIC Wed, 08 Nov 2023 16:05:38 +0000 machine : x86_64 processor : byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8 pandas : 2.2.0.dev0+651.g9aa176687c numpy : 1.26.0 pytz : 2023.3.post1 dateutil : 2.8.2 setuptools : 68.2.2 pip : 23.3.1 Cython : 3.0.5 pytest : 7.4.3 hypothesis : 6.90.0 sphinx : 6.2.1 blosc : None feather : None xlsxwriter : 3.1.9 lxml.etree : 4.9.3 html5lib : 1.1 pymysql : 1.4.6 psycopg2 : 2.9.7 jinja2 : 3.1.2 IPython : 8.17.2 pandas_datareader : None bs4 : 4.12.2 bottleneck : 1.3.7 dataframe-api-compat: None fastparquet : 2023.10.1 fsspec : 2023.10.0 gcsfs : 2023.10.0 matplotlib : 3.8.1 numba : 0.58.1 numexpr : 2.8.7 odfpy : None openpyxl : 3.1.2 pandas_gbq : None pyarrow : 14.0.1 pyreadstat : 1.2.4 python-calamine : None pyxlsb : 1.0.10 s3fs : 2023.10.0 scipy : 1.11.3 sqlalchemy : 2.0.23 tables : 3.9.1 tabulate : 0.9.0 xarray : 2023.11.0 xlrd : 2.0.1 zstandard : 0.22.0 tzdata : 2023.3 qtpy : None pyqt5 : None