-
[x] I have checked that this issue has not already been reported.
-
[x] 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()
Comment From: dsaxton
The mean method hasn't been properly implemented yet for nullable ints, but there is this issue https://github.com/pandas-dev/pandas/issues/34754 and this PR https://github.com/pandas-dev/pandas/pull/34814 open addressing that.
cc @jorisvandenbossche
Comment From: jbrockmendel
The problem isn't with IntegerArray.mean, its with df.T
being all-object dtype.
Comment From: jbrockmendel
Works on main. Could use a test (first check to see if one already exists)