Skip to content

BUG: inconsistent types when applying numpy operations #41756

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

Closed
2 of 3 tasks
janosh opened this issue Jun 1, 2021 · 2 comments
Closed
2 of 3 tasks

BUG: inconsistent types when applying numpy operations #41756

janosh opened this issue Jun 1, 2021 · 2 comments
Labels
API - Consistency Internal Consistency of API/Behavior Bug Compat pandas objects compatability with Numpy or Python functions Reduction Operations sum, mean, min, max, etc. ufuncs __array_ufunc__ and __array_function__

Comments

@janosh
Copy link
Contributor

janosh commented Jun 1, 2021

  • 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

>>> import pandas as pd
>>> import numpy as np
>>> np.mean(pd.DataFrame([[1,2,3],[4,5,6]]), axis=1)
0    2.0
1    5.0
dtype: float64
>>> np.median(pd.DataFrame([[1,2,3],[4,5,6]]), axis=1)
array([2., 5.])

Problem description

The return type when applying numpy ops to data frames should be consistent. In this example, np.mean returns a pd.Series while np.median returns an np.ndarray.

Code that applies dynamically selected numpy operations to pandas dataframes is likely to run into errors if the type of object returned is inconsistent, e.g. expecting to find an index and not finding one in the case of np.median

Expected Output

Both should be pd.Series.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 2cb9652
python : 3.8.5.final.0
python-bits : 64
OS : Darwin
OS-release : 20.5.0
Version : Darwin Kernel Version 20.5.0: Sat May 8 05:10:33 PDT 2021; root:xnu-7195.121.3~9/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.4
numpy : 1.20.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.2
setuptools : 49.6.0.post20200814
Cython : None
pytest : 6.0.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.16.1
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : 0.8.3
fastparquet : None
gcsfs : None
matplotlib : 3.4.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.5.2
sqlalchemy : None
tables : None
tabulate : 0.8.7
xarray : None
xlrd : None
xlwt : None
numba : 0.52.0

@janosh janosh added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 1, 2021
@mzeitlin11
Copy link
Member

Thanks for the report @janosh! Confirmed on current master, investigations to resolve this inconsistency welcome!

@mzeitlin11 mzeitlin11 added API - Consistency Internal Consistency of API/Behavior Compat pandas objects compatability with Numpy or Python functions and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 2, 2021
@mzeitlin11 mzeitlin11 added this to the Contributions Welcome milestone Jul 2, 2021
@mroeschke mroeschke added the Reduction Operations sum, mean, min, max, etc. label Aug 21, 2021
@jbrockmendel jbrockmendel added the ufuncs __array_ufunc__ and __array_function__ label Dec 21, 2021
@jbrockmendel
Copy link
Member

The cause here is that np.mean checks for a 'mean' method and calls that if found, while np.median does not. https://github.com/numpy/numpy/blob/main/numpy/core/fromnumeric.py#L3425

We can get np.median to behave more nicely if we get __array_function__ working, xref #26380. Closing in favor of #26380.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API - Consistency Internal Consistency of API/Behavior Bug Compat pandas objects compatability with Numpy or Python functions Reduction Operations sum, mean, min, max, etc. ufuncs __array_ufunc__ and __array_function__
Projects
None yet
Development

No branches or pull requests

4 participants