-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: pd.Series.argsort misaligned index #42056
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
Comments
With a Series you are, mathematically, associating an
If you were to sort this series naturally you use the
When you perform
The fact that it doesn't do this raises several issues for me:
I would propose that the
With that the following the results are obtained:
where the index has been sorted according to the argsort, so this series has a new meaning: |
Might make sense to return an ndarray instead of a Series? |
well jeff suggested just remove the method. pd Argsort is useless when handling missing data. The above PR fixed all problems and had a better index, but it didnt gain any traction. Only a few people over the years seem to have noticed how bad this method is, and those that did just stopped using it. |
I'd be fine with deprecating and telling users to use |
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
(optional) I have confirmed this bug exists on the master branch of pandas.
Code:
Output:
Problem description
It appears that the argsort output str index order (c, a, b) remains the same as the input data, while the numeric indices (1, 2, 0) are referring to the input data in a different order. This means the str index no longer corresponds with the values (the numeric indices) in the resulting Series. This behavior is confusing, since it sort of looks like c would be the 1st element (0-indexed), a the 2nd, and b the 0th, which is incorrect.
There's actually another example of this here: https://www.geeksforgeeks.org/python-pandas-series-argsort/. The str indices in the result of the argsort don't give any useful information - only the order of the numeric indices is relevant.
Expected Output
I'm not sure if it would make more sense if str indices were reordered to match the numeric indices, like this:
This might be nice, for example, to get the indices of the largest N elements:
order.index[-N:]
. The common workflowseries[series.argsort()]
still seems to work, but I'm not sure of all the other indexing implications.Output of
pd.show_versions()
INSTALLED VERSIONS
commit : 2cb9652
python : 3.7.5.final.0
python-bits : 64
OS : Linux
OS-release : 3.10.0-1160.25.1.el7.x86_64
Version : #1 SMP Tue Apr 13 18:55:45 EDT 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.2.4
numpy : 1.18.5
pytz : 2021.1
dateutil : 2.8.1
pip : 21.1
setuptools : 56.0.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.22.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.4.1
numexpr : None
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : 0.53.1
The text was updated successfully, but these errors were encountered: