Skip to content

BUG: Groupby.median returns absurd results for complex #43770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
3 tasks done
debnathshoham opened this issue Sep 27, 2021 · 3 comments
Open
3 tasks done

BUG: Groupby.median returns absurd results for complex #43770

debnathshoham opened this issue Sep 27, 2021 · 3 comments
Labels
Bug Complex Complex Numbers Error Reporting Incorrect or improved errors from pandas Groupby

Comments

@debnathshoham
Copy link
Member

debnathshoham commented Sep 27, 2021

  • 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 pandas as pd
import numpy as np

data = pd.Series(np.arange(20).reshape(10, 2).dot([1, 2j]))
print(data)
data.groupby(data.index % 2).median()

0    0.00+2.00j
1    2.00+6.00j
2     4.0+10.0j
3     6.0+14.0j
4     8.0+18.0j
5    10.0+22.0j
6    12.0+26.0j
7    14.0+30.0j
8    16.0+34.0j
9    18.0+38.0j
dtype: complex128
/Volumes/SD/pandas-debnathshoham/pandas/core/groupby/ops.py:192: ComplexWarning: Casting complex values to real discards the imaginary part
  values = ensure_float64(values)
Out[14]: 
0     8.0
1    10.0
dtype: float64

Issue Description

groupby.median() returns absurd result with complex numbers.
xref - #43756

Expected Behavior

Two possibilities:

  1. raise with something like "median not defined for complex"
  2. calculate the median separately for real and complex components (which happens on df.median)
In [11]: data
Out[11]: 
0    0.00+2.00j
1    2.00+6.00j
2     4.0+10.0j
3     6.0+14.0j
4     8.0+18.0j
5    10.0+22.0j
6    12.0+26.0j
7    14.0+30.0j
8    16.0+34.0j
9    18.0+38.0j
dtype: complex128

In [12]: data.median()
Out[12]: (9+20j)

Installed Versions

INSTALLED VERSIONS

commit : 619cbf8
python : 3.8.10.final.0
python-bits : 64
OS : Darwin
OS-release : 20.6.0
Version : Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:21 PDT 2021; root:xnu-7195.141.6~3/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 1.4.0.dev0+771.g619cbf8b3e
numpy : 1.21.0
pytz : 2021.1
dateutil : 2.8.1
pip : 21.1.3
setuptools : 49.6.0.post20210108
Cython : 0.29.23
pytest : 6.2.4
hypothesis : 6.14.1
sphinx : 3.5.4
blosc : None
feather : None
xlsxwriter : 1.4.4
lxml.etree : 4.6.3
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.25.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : 1.3.2
fsspec : 2021.05.0
fastparquet : 0.6.3
gcsfs : 2021.05.0
matplotlib : 3.4.2
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : 4.0.1
pyxlsb : 1.0.8
s3fs : 0.4.2
scipy : 1.7.0
sqlalchemy : 1.4.20
tables : 3.6.1
tabulate : 0.8.9
xarray : 0.18.2
xlrd : 2.0.1
xlwt : 1.3.0
numba : 0.53.1

@debnathshoham debnathshoham added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 27, 2021
@debnathshoham debnathshoham changed the title BUG: Groupby.mean returns absurd results for complex BUG: Groupby.median returns absurd results for complex Sep 27, 2021
@debnathshoham debnathshoham added Complex Complex Numbers Groupby labels Sep 27, 2021
@mroeschke mroeschke removed the Needs Triage Issue that has not been reviewed by a pandas team member label Oct 2, 2021
@attack68
Copy link
Contributor

attack68 commented Oct 2, 2021

see mne-tools/mne-python#8879 (comment)

They mention np.median on complex is unsafe since it sorts by real part and returns result without consideration of complex.
The linked package use the marginal approach which I believe takes the median separately over real / imag.

I am inclined to use the same marginal approach, but document it, like the link so calculation is clear.

@debnathshoham
Copy link
Member Author

Little confused about the use-case of calculating median for complex numbers.

Also, df.mean() calculates the median the same way as numpy (i.e. just the real part ignoring complex part).

In [25]: data
Out[25]: 
0    0.0+1.0j
1    1.0+4.0j
2    2.0+3.0j
3    3.0+2.0j
4    4.0+0.0j
dtype: complex128

In [26]: data.median()
Out[26]: (2+3j)

@jbrockmendel
Copy link
Member

raise with something like "median not defined for complex"

I lean towards raising.

@mroeschke mroeschke added the Error Reporting Incorrect or improved errors from pandas label Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Complex Complex Numbers Error Reporting Incorrect or improved errors from pandas Groupby
Projects
None yet
Development

No branches or pull requests

4 participants