Skip to content

BUG: DataFrame.mean(axis=1) fails but Series.mean works incase of pandas Nullable integer dtype #36585

Open
@galipremsagar

Description

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions