Skip to content

BUG: Groupby iteration replaces None with NaN #55419

Closed
@Erotemic

Description

@Erotemic

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugClosing CandidateMay be closeable, needs more eyeballsNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions