Skip to content
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

[BUG] Dataframe.rank() produces wrong results for float columns #32593

Closed
karthikeyann opened this issue Mar 10, 2020 · 3 comments · Fixed by CSCD01-team14/pandas#3 or #38681
Closed

[BUG] Dataframe.rank() produces wrong results for float columns #32593

karthikeyann opened this issue Mar 10, 2020 · 3 comments · Fixed by CSCD01-team14/pandas#3 or #38681
Assignees
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug DataFrame DataFrame data structure
Milestone

Comments

@karthikeyann
Copy link

Code to reproduce the issue

import numpy as np
import pandas as pd

indx = np.array([5, 4, 3, 2, 1, 6, 7, 8, 9, 10])
col1 = np.array([5, 4, 3, 5, 8, 5, 2, 1, 6, 6])
col2 = np.array([5, 4, np.nan, 5, 8, 5, np.inf, np.nan, 6, -np.inf])
pdf = pd.DataFrame(index=indx)
pdf["col1"] = col1.astype('f8')
pdf["col2"] = col2.astype('f8')

ranked_df = pdf.rank()

ranked_series = pd.DataFrame(index=indx)
ranked_series["col1"] = pdf["col1"].rank()
ranked_series["col2"] = pdf["col2"].rank()

print(ranked_df.equals(ranked_series))
#False

Problem description

Dataframe rank produces individual column ranks. But when float column is present, individual ranks are not equal to Dataframe rank.

Expected Output

True

Output of pd.show_versions()

pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.8.1.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-72-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.1
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.0.0.post20200309
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.12.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@dsaxton
Copy link
Member

dsaxton commented Mar 10, 2020

From what I can tell the difference has to do with the handling of np.inf? If so this is a slightly smaller example:

In [1]: import numpy as np                                                                                                                                                                                                                

In [2]: import pandas as pd                                                                                                                                                                                                               

In [3]: values = [-np.inf, 0, np.inf]                                                                                                                                                                                                     

In [4]: print(pd.Series(values).rank())                                                                                                                                                                                                   
0    1.0
1    2.0
2    3.0
dtype: float64

In [5]: print(pd.DataFrame({"a": values}).rank())                                                                                                                                                                                         
     a
0  1.0
1  2.0
2  NaN

Does seem odd that np.inf doesn't rank as the largest value in the DataFrame case.

@WillAyd WillAyd added Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug DataFrame DataFrame data structure labels Mar 11, 2020
@WillAyd WillAyd added this to the Contributions Welcome milestone Mar 11, 2020
@letapxad
Copy link

take

@peterpanmj
Copy link
Contributor

From what I can tell the difference has to do with the handling of np.inf? If so this is a slightly smaller example:

In [1]: import numpy as np                                                                                                                                                                                                                

In [2]: import pandas as pd                                                                                                                                                                                                               

In [3]: values = [-np.inf, 0, np.inf]                                                                                                                                                                                                     

In [4]: print(pd.Series(values).rank())                                                                                                                                                                                                   
0    1.0
1    2.0
2    3.0
dtype: float64

In [5]: print(pd.DataFrame({"a": values}).rank())                                                                                                                                                                                         
     a
0  1.0
1  2.0
2  NaN

Does seem odd that np.inf doesn't rank as the largest value in the DataFrame case.

A very good example. It is related to an old issue #6945. When missing values and inf values appear in one Series, the ranks will be wrong. I've worked on it before. It seems that DataFrame rank still cannot handle it, might be a regression.

@jreback jreback modified the milestones: Contributions Welcome, 1.2.1, 1.3 Dec 27, 2020
rapids-bot bot pushed a commit to rapidsai/cudf that referenced this issue Jan 28, 2021
Closes #7174 

This PR adds support for `numeric_only` field for `Dataframe.rank()`  and `Series.rank()`. When user specifies `numeric_only=True`, only the numerical data type columns are selected to construct a cudf object and passed to lower level for processing.

Two minor refactors are also included in this PR:

- This PR refactors internal API of `Frame._get_columns_by_label`, which now supports dispatching to this method from both `Dataframe` and `Series`. 
- This PR refactors `test_rank.py`, moving test functions inside class `TestRank` out as top level functions. All test variables shared among test cases are moved to a `pytests.fixture` method. A `Dataframe.rank` test case that expects to raise due to a [pandas bug](pandas-dev/pandas#32593) is now captured under `pytest.raises`.

Authors:
  - Michael Wang (@isVoid)

Approvers:
  - Ashwin Srinath (@shwina)
  - @brandon-b-miller

URL: #7213
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 DataFrame DataFrame data structure
Projects
None yet
6 participants