Skip to content

BUG: Can't compute group mean of complex values #43701

Closed
@goyodiaz

Description

@goyodiaz

  • 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 master branch of pandas.

Reproducible Example

import numpy as np
import pandas as pd

rng = np.random.default_rng(42)
data = pd.Series(rng.uniform(size=(10, 2)).dot([1, 1j]))
gr = data.groupby(data.index % 2)
gr.mean()  # TypeError: No matching signature found

Issue Description

gr.mean() raises TypeError: No matching signature found when the underlying dtype is complex128. The other complex dtypes present the same behavior.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/goyo/.local/lib/python3.9/site-packages/pandas/core/groupby/groupby.py", line 1684, in mean
    result = self._cython_agg_general(
  File "/home/goyo/.local/lib/python3.9/site-packages/pandas/core/groupby/generic.py", line 352, in _cython_agg_general
    result = array_func(objvals)
  File "/home/goyo/.local/lib/python3.9/site-packages/pandas/core/groupby/generic.py", line 340, in array_func
    result = self.grouper._cython_operation(
  File "/home/goyo/.local/lib/python3.9/site-packages/pandas/core/groupby/ops.py", line 978, in _cython_operation
    return cy_op.cython_operation(
  File "/home/goyo/.local/lib/python3.9/site-packages/pandas/core/groupby/ops.py", line 639, in cython_operation
    return self._cython_op_ndim_compat(
  File "/home/goyo/.local/lib/python3.9/site-packages/pandas/core/groupby/ops.py", line 481, in _cython_op_ndim_compat
    res = self._call_cython_op(
  File "/home/goyo/.local/lib/python3.9/site-packages/pandas/core/groupby/ops.py", line 557, in _call_cython_op
    func(result, counts, values, comp_ids, min_count)
  File "pandas/_libs/groupby.pyx", line 668, in pandas._libs.groupby.__pyx_fused_cpdef
TypeError: No matching signature found

Expected Behavior

Since data.mean() works I would expect that it also works in the GroupBy object. The expected result is:

>>> gr.mean()
0    0.438939+0.550293j
1    0.652316+0.653820j
dtype: complex128

Just like .sum():

>>> gr.sum()  # this does work.
0    2.194697+2.751466j
1    3.261581+3.269100j
dtype: complex128

Installed Versions

INSTALLED VERSIONS

commit : 73c6825
python : 3.9.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.11.0-34-generic
Version : #36-Ubuntu SMP Thu Aug 26 19:22:09 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.3
numpy : 1.21.2
pytz : 2021.1
dateutil : 2.8.2
pip : 21.2.4
setuptools : 58.0.4
Cython : None
pytest : None
hypothesis : None
sphinx : 3.5.3
blosc : None
feather : None
xlsxwriter : 3.0.1
lxml.etree : 4.6.3
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.27.0
pandas_datareader: None
bs4 : 4.10.0
bottleneck : 1.3.2
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.4.3
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.8
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.7.1
sqlalchemy : None
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : 1.3.0
numba : None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions