Skip to content

BUG: groupy sum on string column with NA returns results with inconsistent types #53568

Open
@ngoldbaum

Description

@ngoldbaum

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

import pandas as pd
df = pd.DataFrame(
    {
        "A": [1, 1, 2, 2, 3, 3, 1, 4],
        "B": ['B', 'B', pd.NA, pd.NA, 'A', 'A', 'B', 'C'],
        "C": [1, 1, 1, 1, 1, 1, 1, 1],
    }
)
print(df.groupby("A").sum()['B'][2])
print(type(df.groupby("A").sum()['B'][2]))
print(type(df.groupby("A").sum()['B'][3]))

Issue Description

Currently this prints:

0
<class 'int'>
<class 'str'>

Notably if you create the dataframe like this:

df = pd.DataFrame(
    {
        "A": [1, 1, 2, 2, 3, 3, 1, 4],
        "B": pd.array(['B', 'B', pd.NA, pd.NA, 'A', 'A', 'B', 'C'], dtype='string[python]')
        "C": [1, 1, 1, 1, 1, 1, 1, 1],
    }
)

I get back 0 as a string when I do the groupby('A').sum(), as expected.

Expected Behavior

I would expect the test script to print:

'0'
<class 'str'>
<class 'str'>

Installed Versions

INSTALLED VERSIONS

commit : 965ceca
python : 3.10.9.final.0
python-bits : 64
OS : Linux
OS-release : 5.19.0-42-generic
Version : #43~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Apr 21 16:51:08 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.0.2
numpy : 1.25.0rc1+93.g95343a3e6
pytz : 2022.7.1
dateutil : 2.8.2
setuptools : 65.5.0
pip : 23.1.2
Cython : 0.29.35
pytest : 7.3.0
hypothesis : 6.68.2
sphinx : 6.1.3
blosc : 1.11.1
feather : None
xlsxwriter : 3.0.9
lxml.etree : 4.9.2
html5lib : 1.1
pymysql : 1.0.3
psycopg2 : 2.9.6
jinja2 : 3.1.2
IPython : 8.11.0
pandas_datareader: None
bs4 : 4.12.2
bottleneck : 1.3.7
brotli :
fastparquet : 2023.2.0
fsspec : 2023.4.0
gcsfs : 2023.4.0
matplotlib : 3.6.3
numba : None
numexpr : 2.8.4
odfpy : None
openpyxl : 3.1.0
pandas_gbq : None
pyarrow : 11.0.0
pyreadstat : 1.2.1
pyxlsb : 1.0.10
s3fs : 2023.4.0
scipy : 1.10.1
snappy :
sqlalchemy : 2.0.7
tables : 3.8.0
tabulate : 0.9.0
xarray : 2023.4.2
xlrd : 2.0.1
zstandard : 0.21.0
tzdata : 2022.7
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugGroupbyMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolateNeeds DiscussionRequires discussion from core team before further action

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions