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: Groupby iteration replaces None with NaN #55419

Closed
2 of 3 tasks
Erotemic opened this issue Oct 6, 2023 · 2 comments
Closed
2 of 3 tasks

BUG: Groupby iteration replaces None with NaN #55419

Erotemic opened this issue Oct 6, 2023 · 2 comments
Labels
Bug Closing Candidate May be closeable, needs more eyeballs Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@Erotemic
Copy link
Contributor

Erotemic commented Oct 6, 2023

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

df = pd.DataFrame([
    {'a': None, 'b': 4, 'c': 1},
    {'a': None, 'b': 1, 'c': 1},
    {'a': None, 'b': 1, 'c': 2},
    {'a': 1, 'b': 2, 'c': 2},
    {'a': 1, 'b': 3, 'c': 2},
], dtype=object)
group_keys = ['a', 'c']
grouped = df.groupby(group_keys, dropna=False)
for group_vals, group in grouped:
    group_key1 = dict(zip(group_keys, group_vals))
    group_key2 = group.iloc[0][group_keys].to_dict()
    print('---')
    print(f'group_key1={group_key1}')
    print(f'group_key2={group_key2}')

This results in

---
group_key1={'a': nan, 'c': 1}
group_key2={'a': None, 'c': 1}
---
group_key1={'a': nan, 'c': 2}
group_key2={'a': None, 'c': 2}
---
group_key1={'a': 1.0, 'c': 2}
group_key2={'a': 1, 'c': 2}

The problem is I would expect group_key1 to be the same as group_key2.

Issue Description

When iterating over a pandas.core.groupby.generic.DataFrameGroupBy object, the returned group values replace None with nan.

Expected Behavior

I would have expected group_key1 and group_key2 to be the same in each iteration. Namely, I would think the value for "a" should always be "None" because that's what's in my DataFrame, but the set of unique values returned when iterating over the result of groupby seems to be replacing Nones with nans.

I did do a search for this issue, and I didn't find anything that matched it, but I'm also not confident that I didn't miss anything. I'm also not sure that this isn't expected behavior. Looking at the docstring for pandas.core.groupby.generic.DataFrameGroupBy or pandas.core.groupby.generic.DataFrameGroupBy.__iter__ didn't show anything. If this is expected behavior, any pointer to documenation would be helpful.

In the meantime simply using the first set of values in the returned group does seem to work fine, but it's odd to me the tuple of "group values" doesn't come directly from the underlying dataframe.

Installed Versions

INSTALLED VERSIONS

commit : e86ed37
python : 3.12.0.final.0
python-bits : 64
OS : Linux
OS-release : 6.2.0-33-generic
Version : #33~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Sep 7 10:33:52 UTC 2
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.1.1
numpy : 1.26.0
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : None
pip : 23.2.1
Cython : None
pytest : 7.4.2
hypothesis : None
sphinx : 7.2.6
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.16.1
pandas_datareader : None
bs4 : None
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@Erotemic Erotemic added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 6, 2023
@Erotemic Erotemic changed the title BUG: BUG: Groupby iteration replaces None with NaN Oct 6, 2023
@paulreece
Copy link
Contributor

This seems like it is intended behavior please see here:

def filter(self, func, dropna: bool = True, *args, **kwargs):
"""
Filter elements from groups that don't satisfy a criterion.
Elements from groups are filtered if they do not satisfy the
boolean criterion specified by func.
Parameters
----------
func : function
Criterion to apply to each group. Should return True or False.
dropna : bool
Drop groups that do not pass the filter. True by default; if False,
groups that evaluate False are filled with NaNs.

One of the maintainers will be able to give a more detailed explanation of why this is the case.

@paulreece paulreece added the Closing Candidate May be closeable, needs more eyeballs label Oct 6, 2023
@Erotemic Erotemic closed this as completed Oct 6, 2023
@rhshadrach
Copy link
Member

@Erotemic - I think this is essentially #48476.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Closing Candidate May be closeable, needs more eyeballs Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

3 participants