Skip to content

BUG: nunique not ignoring both None and np.nan #37566

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
1 of 3 tasks
Tomer-Eliahu opened this issue Nov 1, 2020 · 1 comment · Fixed by #37607
Closed
1 of 3 tasks

BUG: nunique not ignoring both None and np.nan #37566

Tomer-Eliahu opened this issue Nov 1, 2020 · 1 comment · Fixed by #37607
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Milestone

Comments

@Tomer-Eliahu
Copy link

  • 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.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas as pd
import numpy as np
pd.Series(['yes','yes', None, np.nan]).nunique(dropna = True)

Problem description

The output was 2.
It seems that nunique can only ignore None or np.nan but not both together. This seems odd to me and I think it
should either be changed or the docs should reflect this.

Expected Output

I expected to get 1 as the result but I got 2.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : db08276
python : 3.7.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.49+
Version : #1 SMP Sun Oct 18 19:43:35 PDT 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : C.UTF-8
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.3
numpy : 1.18.5
pytz : 2019.3
dateutil : 2.8.1
pip : 20.2.4
setuptools : 46.1.3.post20200325
Cython : 0.29.21
pytest : 5.4.1
hypothesis : 5.10.0
sphinx : 3.0.2
blosc : None
feather : 0.4.1
xlsxwriter : 1.2.8
lxml.etree : 4.5.0
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.13.0
pandas_datareader: 0.9.0
bs4 : 4.9.0
bottleneck : 1.3.2
fsspec : 0.8.4
fastparquet : None
gcsfs : 0.6.1
matplotlib : 3.2.1
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : 0.16.0
pytables : None
pyxlsb : None
s3fs : 0.5.1
scipy : 1.4.1
sqlalchemy : 1.3.16
tables : 3.6.1
tabulate : 0.8.7
xarray : 0.16.1
xlrd : 1.2.0
xlwt : 1.3.0
numba : 0.48.0

@Tomer-Eliahu Tomer-Eliahu added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 1, 2020
@jorisvandenbossche
Copy link
Member

@tomereliahu Thanks for the report!

That's indeed clearly a bug, and looking at the code of nunique, it's also clear where it is coming from:

pandas/pandas/core/base.py

Lines 1035 to 1039 in 337bf20

uniqs = self.unique()
n = len(uniqs)
if dropna and isna(uniqs).any():
n -= 1
return n

So it always subtracts 1, instead of checking how many NA-like values are present in the uniques. So we should be subtracting isna(uniqs).sum() instead, I think.

Pull requests welcome!

@jorisvandenbossche jorisvandenbossche removed the Needs Triage Issue that has not been reviewed by a pandas team member label Nov 3, 2020
@jorisvandenbossche jorisvandenbossche added this to the Contributions Welcome milestone Nov 3, 2020
@jreback jreback modified the milestones: Contributions Welcome, 1.2 Nov 4, 2020
@jreback jreback added the Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate label Nov 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants