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: DataFrame.mean(axis=1) fails but Series.mean works incase of pandas Nullable integer dtype #36585

Open
2 of 3 tasks
galipremsagar opened this issue Sep 23, 2020 · 2 comments
Labels
Bug NA - MaskedArrays Related to pd.NA and nullable extension arrays Reduction Operations sum, mean, min, max, etc.

Comments

@galipremsagar
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.

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


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

>>> import pandas as pd
>>> df = pd.DataFrame({"a":[1,2,3, 4], "b":pd.Series([1, 2, 4, None], dtype=pd.Int64Dtype())})
>>> df
   a     b
0  1     1
1  2     2
2  3     4
3  4  <NA>
>>> df.mean()
a    2.500000
b    2.333333
dtype: float64
>>> df.mean(skipna=False)
a     2.5
b    <NA>
dtype: object
>>> df.iloc[3]
a       4
b    <NA>
Name: 3, dtype: Int64
>>> df.iloc[3].mean(skipna=True)
4.0
>>> df.iloc[3].mean(skipna=False)
<NA>
>>> df.mean(axis=1, skipna=False)
Traceback (most recent call last):
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/nanops.py", line 1415, in _ensure_numeric
    x = x.astype(np.complex128)
TypeError: must be real number, not NAType

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/frame.py", line 8649, in _reduce
    result = func(values)
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/frame.py", line 8570, in func
    return op(values, axis=axis, skipna=skipna, **kwds)
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/nanops.py", line 71, in _f
    return f(*args, **kwargs)
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/nanops.py", line 129, in f
    result = alt(values, axis=axis, skipna=skipna, **kwds)
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/nanops.py", line 563, in nanmean
    the_sum = _ensure_numeric(values.sum(axis, dtype=dtype_sum))
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/nanops.py", line 1418, in _ensure_numeric
    x = x.astype(np.float64)
TypeError: float() argument must be a string or a number, not 'NAType'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/nanops.py", line 1415, in _ensure_numeric
    x = x.astype(np.complex128)
TypeError: must be real number, not NAType

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/generic.py", line 11466, in stat_func
    func, name=name, axis=axis, skipna=skipna, numeric_only=numeric_only
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/frame.py", line 8660, in _reduce
    result = func(values)
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/frame.py", line 8570, in func
    return op(values, axis=axis, skipna=skipna, **kwds)
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/nanops.py", line 71, in _f
    return f(*args, **kwargs)
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/nanops.py", line 129, in f
    result = alt(values, axis=axis, skipna=skipna, **kwds)
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/nanops.py", line 563, in nanmean
    the_sum = _ensure_numeric(values.sum(axis, dtype=dtype_sum))
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/nanops.py", line 1418, in _ensure_numeric
    x = x.astype(np.float64)
TypeError: float() argument must be a string or a number, not 'NAType'

Problem description

When there is a row-wise mean calculation of a nullable interger dtype the operation fails. But the same operation works correctly on a series by doing an iloc on the row.

Expected Output

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 2a7d332
python : 3.7.3.final.0
python-bits : 64
OS : Darwin
OS-release : 19.6.0
Version : Darwin Kernel Version 19.6.0: Thu Jun 18 20:49:00 PDT 2020; root:xnu-6153.141.1~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 1.1.2
numpy : 1.17.3
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.3
setuptools : 50.3.0
Cython : None
pytest : None
hypothesis : 5.29.0
sphinx : None
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 : 1.0.1
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@galipremsagar galipremsagar added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 23, 2020
@jbrockmendel jbrockmendel added the Reduction Operations sum, mean, min, max, etc. label Sep 23, 2020
@dsaxton
Copy link
Member

dsaxton commented Sep 23, 2020

The mean method hasn't been properly implemented yet for nullable ints, but there is this issue #34754 and this PR #34814 open addressing that.

cc @jorisvandenbossche

@dsaxton dsaxton added NA - MaskedArrays Related to pd.NA and nullable extension arrays and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 23, 2020
@jbrockmendel
Copy link
Member

The problem isn't with IntegerArray.mean, its with df.T being all-object dtype.

@mroeschke mroeschke changed the title BUG: DataFrame.mean fails but Series.mean works incase of pandas Nullable integer dtype BUG: DataFrame.mean(axis=1) fails but Series.mean works incase of pandas Nullable integer dtype Jul 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug NA - MaskedArrays Related to pd.NA and nullable extension arrays Reduction Operations sum, mean, min, max, etc.
Projects
None yet
Development

No branches or pull requests

3 participants