Skip to content

BUG: DataFrame.nlargest() returns incorrect result when DataFrame has non-unique index #14846

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
Dr-Irv opened this issue Dec 9, 2016 · 3 comments

Comments

@Dr-Irv
Copy link
Contributor

Dr-Irv commented Dec 9, 2016

Code Sample, a copy-pastable example if possible

import pandas as pd
df=pd.DataFrame({'variable': ['a','a','b','b','c','c'],
                 'value' : [1000,2000,10,20,100,200]},
                 index=[1,2]*3)
df.nlargest(3,'value')

Problem description

The result should only have 3 rows. If the index has unique values, it is correct. But when the index has duplicate values, the incorrect result is produced.

The example produces the following output:

   value variable
2   2000        a
2   2000        a
1   1000        a
2    200        c
2    200        c
1    100        c
2     20        b
2     20        b
1     10        b

Expected Output

   value variable
2  2000       a
1  1000       a
2  200        c

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.5.2.final.0 python-bits: 64 OS: Windows OS-release: 10 machine: AMD64 processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel byteorder: little LC_ALL: None LANG: None LOCALE: None.None

pandas: 0.19.0
nose: None
pip: 8.1.2
setuptools: 27.2.0
Cython: None
numpy: 1.11.2
scipy: None
statsmodels: None
xarray: None
IPython: 5.1.0
sphinx: None
patsy: None
dateutil: 2.5.3
pytz: 2016.7
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: None
openpyxl: None
xlrd: 1.0.0
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.5.1
html5lib: 0.999
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None
pandas_datareader: None

@chris-b1
Copy link
Contributor

chris-b1 commented Dec 9, 2016

This looks to be a duplicate of #13412 - which is fixed on master

@jreback jreback closed this as completed Dec 9, 2016
@gordonda
Copy link

I have a similar problem with a slight change to the example above

df=pd.DataFrame({'variable': ['a','a','b','b','c','c'],
                 'value' : [1000,2000,1000,20,100,200]},
                 index=[1,2]*3)
df.nlargest(3, 'value')

An expected output would be

    value variable
2   2000        a
1   1000        a
1   1000        b

Instead it is

   value variable
2   2000        a
1   1000        a
1   1000        b
1   1000        a
1   1000        b

It seems that duplicate values in the column specified in nlargest leads to this behaviour

@jreback
Copy link
Contributor

jreback commented Feb 21, 2017

@gordonda see #15297 which is the same

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants