Skip to content
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

BUG: Inconsistent return types for PandasArray and Series reductions #35475

Open
3 tasks done
marco-neumann-by opened this issue Jul 30, 2020 · 2 comments
Open
3 tasks done
Labels
API - Consistency Internal Consistency of API/Behavior Bug Reduction Operations sum, mean, min, max, etc.

Comments

@marco-neumann-by
Copy link

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

  • I have confirmed this bug exists on the latest version of pandas (1.1.0)

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


Code Sample, a copy-pastable example

import numpy as np
import pandas as pd
from pandas.arrays import PandasArray


# ==== array: empty case ====
# The following could also be archived with public-only APIs:
#   array_empty = pd.Series([], dtype=float).array
array_empty = PandasArray._from_sequence([], dtype=float)

assert type(array_empty.mean()) == float
assert type(array_empty.median()) == float

assert type(array_empty.sum()) == np.float64

assert type(array_empty.min()) == type(pd.NA)
assert type(array_empty.max()) == type(pd.NA)


# ==== array: single value case ====
# The following could also be archived with public-only APIs:
#   array_single = pd.Series([1.0], dtype=float).array
array_single = PandasArray._from_sequence([1.0], dtype=float)

if pd.__version__ == "1.1.0":
    assert type(array_single.mean()) == np.float64
    assert type(array_single.median()) == np.float64
else:
    # master (3b1d4f1eef46a45b7d32694bb263a2e40a933e74)
    assert type(array_single.mean()) == float
    assert type(array_single.median()) == float

assert type(array_single.sum()) == np.float64
assert type(array_single.min()) == np.float64
assert type(array_single.max()) == np.float64


# ==== series: empty case ====
series_empty = pd.Series([], dtype=float)

assert type(series_empty.mean()) == float
assert type(series_empty.median()) == float

assert type(series_empty.sum()) == np.float64

assert type(series_empty.min()) == float
assert type(series_empty.max()) == float


# ==== series: single value case ====
series_single = pd.Series([1.0], dtype=float)

if pd.__version__ == "1.1.0":
    assert type(series_single.mean()) == np.float64
    assert type(series_single.median()) == np.float64
else:
    # master (3b1d4f1eef46a45b7d32694bb263a2e40a933e74)
    assert type(series_single.mean()) == float
    assert type(series_single.median()) == float

assert type(series_single.sum()) == np.float64

if pd.__version__ == "1.1.0":
    assert type(series_single.min()) == np.float64
    assert type(series_single.max()) == np.float64
else:
    # master (3b1d4f1eef46a45b7d32694bb263a2e40a933e74)
    assert type(series_single.min()) == float
    assert type(series_single.max()) == float

Problem description

As shown above, the return types for PandasArrays is highly inconsistent within the case as well as compared to the non-empty case.

This also extends to the Series cases, where I don't understand the behavior at all, especially when looking the outputs from master.

Expected Output

A single output type (either np.float64) or pd.NA? To be honest, I am not sure what the expected outcome is, but it would be good to clarify that because other extension arrays should probably implement the same behavior.

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit           : d9fff2792bf16178d4e450fe7384244e50635733
python           : 3.8.5.final.0
python-bits      : 64
OS               : Darwin
OS-release       : 19.6.0
Version          : Darwin Kernel Version 19.6.0: Sun Jul  5 00:43:10 PDT 2020; root:xnu-6153.141.1~9/RELEASE_X86_64
machine          : x86_64
processor        : i386
byteorder        : little
LC_ALL           : en_US.UTF-8
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.1.0
numpy            : 1.19.1
pytz             : 2020.1
dateutil         : 2.8.1
pip              : 20.1.1
setuptools       : 47.1.0
Cython           : None
pytest           : 6.0.0
hypothesis       : None
sphinx           : 3.1.2
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : 2.11.2
IPython          : None
pandas_datareader: None
bs4              : None
bottleneck       : None
fsspec           : None
fastparquet      : None
gcsfs            : None
matplotlib       : None
numexpr          : None
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pytables         : None
pyxlsb           : None
s3fs             : None
scipy            : None
sqlalchemy       : None
tables           : None
tabulate         : None
xarray           : None
xlrd             : None
xlwt             : None
numba            : 0.50.1
@marco-neumann-by marco-neumann-by added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 30, 2020
@arw2019
Copy link
Member

arw2019 commented Jul 31, 2020

xref #28095

@jbrockmendel jbrockmendel added API - Consistency Internal Consistency of API/Behavior Numeric Operations Arithmetic, Comparison, and Logical operations and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 3, 2020
@jbrockmendel jbrockmendel added the Reduction Operations sum, mean, min, max, etc. label Sep 21, 2020
@CormacDC
Copy link

Hello, I would like to work on this issue but it would appear that we need more information. Are the inconsistent return data types intentional? If not, what are they supposed to be? After reading #28095 and #29556, it would appear that these inconsistent return data types were intentional but I'm still not sure.

@mroeschke mroeschke added Bug and removed Numeric Operations Arithmetic, Comparison, and Logical operations labels Aug 8, 2021
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 Reduction Operations sum, mean, min, max, etc.
Projects
None yet
Development

No branches or pull requests

5 participants