Skip to content

GroupBy handles non-numeric data differently for different aggregation functions #20871

Closed
@Dominik1123

Description

@Dominik1123

Code Sample, a copy-pastable example if possible

import pandas as pd

s = pd.Series(
    [1, 2, 3, 4],
    index=['a', 'a', 'b', 'b'],
    dtype=object
)
print(s.groupby(lambda x: x).sum())  # Works fine.
print(s.groupby(lambda x: x).mean())  # Raises `DataError`.

Problem description

GroupBy objects behave differently for different aggregation functions when used on non-numeric data types. mean (amongst others) re-raises on GroupByError while sum (amongst others) falls back on np.sum.

The expected behavior would be that all such functions either fall back on their numpy equivalent or re-raise on DataError for non-numeric data.

Expected Output

All such functions should either fall back on their numpy equivalent or re-raise on DataError for non-numeric data.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-119-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.22.0
pytest: None
pip: 9.0.1
setuptools: 34.2.0
Cython: None
numpy: 1.14.0
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 5.2.2
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.0.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: 0.7.3
lxml: None
bs4: 4.4.1
html5lib: 0.9999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.5
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions