Skip to content

Applying groupby and aggregating with nunique over float column with nulls unexpectedly changes dataframe #32165

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
cuckookernel opened this issue Feb 21, 2020 · 1 comment

Comments

@cuckookernel
Copy link

Code Sample, a copy-pastable example if possible

import pandas as pd
import numpy as np
print( pd.__version__ )  # 1.0.1
df = pd.DataFrame( [[1, 0], [2, np.nan], [3, np.nan]], columns=['a', 'b'] )
print( df )
#   a    b
# 0  1  0.0
# 1  2  NaN
# 2  3  NaN
unused = df.groupby('a').agg({'b': 'nunique'})     # this shouldn't change anything in df, right?
print( df ) # prints out
#   a             b
# 0  1  0.000000e+00
# 1  2 -9.223372e+18
# 2  3 -9.223372e+18
# what happened to my nans?

Problem description

For some reason the agg, groupby operation is changing the orginal nan values in the b column
to a special float value.
In the previous version of pandas I was using, this didn't happen.

Expected Output

The expected output of the print should the same as the one of the first print as groupby + agg should never change the input df (right?)

Output of pd.show_versions()

[paste the output of ``pd.show_versions()`` here below this line]

INSTALLED VERSIONS

commit : None
python : 3.7.6.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-70-generic
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : sv_SE.UTF-8
LOCALE : sv_SE.UTF-8
pandas : 1.0.1
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 45.2.0.post20200210
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.8.4 (dt dec pq3 ext lo64)
jinja2 : 2.11.1
IPython : None
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 : 1.0.6
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.13
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@MarcoGorelli
Copy link
Member

Thanks @cuckookernel for the report!

This is indeed a bug - closing though as we have it reported in #31950

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

2 participants