Skip to content

BUG: Groupby agg on decimal,json extension arrays changes dtype to object #39098

Closed
@BryanCutler

Description

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

import decimal
from pandas.tests.extension.decimal import DecimalArray
import pandas as pd

b = decimal.Decimal("1.0")
a = decimal.Decimal("0.0")
c = decimal.Decimal("2.0")
na = decimal.Decimal("NaN")

df = pd.DataFrame({
    'key': [1, 1, 2, 2, 3, 3, 1, 4], 
    'val': DecimalArray([b, b, na, na, a, a, b, c])
})

result = df.groupby("key").agg({'val': 'first'})

print(result['val').dtype)
# object

Problem description

When performing a groupby().agg() on extension arrays DecimalArray or JsonArray, if a NULL value is present, the resulting aggregation will be converted to an object dtype.

Expected Output

Resulting series should be the same dtype as the input array, and any NULL values should be as defined in the extension dtype.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 1c296b2
python : 3.8.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-58-generic
Version : #64-Ubuntu SMP Wed Dec 9 08:16:25 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : C.UTF-8
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.0.dev0+357.g1c296b20f.dirty
numpy : 1.19.4
pytz : 2020.4
dateutil : 2.8.1
pip : 20.0.2
setuptools : 49.6.0.post20201009
Cython : 0.29.21
pytest : 6.1.2
hypothesis : 5.41.5
sphinx : 3.3.1
blosc : None
feather : None
xlsxwriter : 1.3.7
lxml.etree : 4.6.2
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.19.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : 1.3.2
fsspec : 0.8.4
fastparquet : 0.4.1
gcsfs : 0.7.1
matplotlib : 3.3.3
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.5
pandas_gbq : None
pyarrow : 2.0.0
pyxlsb : None
s3fs : 0.4.2
scipy : 1.5.3
sqlalchemy : 1.3.20
tables : 3.6.1
tabulate : 0.8.7
xarray : 0.16.2
xlrd : 1.2.0
xlwt : 1.3.0
numba : 0.52.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    ApplyApply, Aggregate, Transform, MapBugExtensionArrayExtending pandas with custom dtypes or arrays.Groupby

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions