Skip to content

BUG: Index.sort_values with natsort key #56081

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
speleo3 opened this issue Nov 20, 2023 · 3 comments · Fixed by #58148
Closed
3 tasks done

BUG: Index.sort_values with natsort key #56081

speleo3 opened this issue Nov 20, 2023 · 3 comments · Fixed by #58148
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug

Comments

@speleo3
Copy link
Contributor

speleo3 commented Nov 20, 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
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
@speleo3 speleo3 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 20, 2023
@hvsesha
Copy link

hvsesha commented Nov 22, 2023

@speleo3 I found out the issue here
@Dhayanidhi-M Thanks for your help
In Sorting.py
Nargsort function casting is the problem

#56081 BugFix Casting
#items = ensure_key_mapped(items, key)
items=cast(items, ensure_key_mapped(items, key))._values

cast(type(items),ensure_key_mapped(items, key))._values
Kindly check and revert back

hvsesha added a commit to hvsesha/pandas that referenced this issue Nov 23, 2023
@phofl phofl added Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 29, 2023
@bdwzhangumich
Copy link
Contributor

take

@bdwzhangumich bdwzhangumich removed their assignment Apr 3, 2024
@yuanx749
Copy link
Contributor

yuanx749 commented Apr 4, 2024

The problem is that applying natsort.natsort_key to Index results in MultiIndex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants