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

Groupby converts None to NaN #32800

Closed
BayerSe opened this issue Mar 18, 2020 · 5 comments · Fixed by #33462
Closed

Groupby converts None to NaN #32800

BayerSe opened this issue Mar 18, 2020 · 5 comments · Fixed by #33462
Labels
Groupby Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@BayerSe
Copy link

BayerSe commented Mar 18, 2020

Code Sample, a copy-pastable example if possible

import pandas as pd

df = pd.DataFrame.from_dict({
    'id': ['a'],
    'value': [None]
})

df_grouped = df.groupby('id', as_index=False).first()

print(df)
print(df_grouped)

Output:

  id value
0  a  None
  id  value
0  a  NaN

Problem description

Since at least version 1.0.2, the type of df_grouped is NaN. In Version 0.25.3, the type was None.
This breaks my code since I later check for this value using if var is None, which is False when var is NaN instead of None.

Expected Output

  id value
0  a  None
  id  value
0  a  None

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.8.1.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-88-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.2
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.0.0
Cython : None
pytest : 5.4.1
hypothesis : None
sphinx : 2.4.4
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.1
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.2.0
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.4.1
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.15
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@hkishn
Copy link

hkishn commented Mar 18, 2020

@BayerSe
As a workaround you can "None" instead of None.
df = pd.DataFrame.from_dict({
'id': ['a'],
'value': ["None"]
})
This should give the output of None instead of Nan.
I will also check the release document of pandas 1.0.2 for this change
Hope this helps.

@BayerSe
Copy link
Author

BayerSe commented Mar 18, 2020

Thanks @hkishn but this won't help because "None" is a string and not an empty value

@BayerSe
Copy link
Author

BayerSe commented Mar 26, 2020

Gentle bump -
could anyone from the team state whether I should adjust my code or if that is something that would be fixed in a future version?

@TomAugspurger
Copy link
Contributor

Not sure. Are you able to bisect to the commit that caused the change?

@simonjayhawkins
Copy link
Member

20a84a5 is the first bad commit
commit 20a84a5
Author: Daniel Saxton 2658661+dsaxton@users.noreply.github.com
Date: Sun Feb 23 08:58:59 2020 -0600

BUG: Avoid ambiguous condition in GroupBy.first / last (#32124)

@simonjayhawkins simonjayhawkins added Regression Functionality that used to work in a prior pandas version Groupby Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate labels Mar 26, 2020
@jreback jreback added this to the 1.1 milestone Apr 10, 2020
@simonjayhawkins simonjayhawkins modified the milestones: 1.1, 1.0.4 May 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Groupby Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants