Skip to content

Dataframe.mean with numeric_only=False results in error with strings #26927

Closed
@smangham

Description

@smangham

Code Sample, a copy-pastable example if possible

import pandas as pd
df = pd.DataFrame({
    'A': [0, 1, 2], 'B': ['a', 'b', 'c'], 'C': [4, 5, 6]
})
df.mean(axis=0, numeric_only=False, skipna=False)

Problem description

Instead of outputting a NaN for a non-numeric column of strings when trying to take the mean, it instead throws TypeError: could not convert string 'abc' to float.

Expected Output

I would expect this to output a series with values [1, NaN, 5].

My work-around is currently df.apply(pd.to_numeric, args=['coerce']).mean(axis=0, skipna=False), which outputs the expected result.

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.6.7.final.0 python-bits: 64 OS: Linux OS-release: 4.15.0-51-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_GB.UTF-8 LOCALE: en_GB.UTF-8

pandas: 0.24.2
pytest: None
pip: 19.1.1
setuptools: 40.6.3
Cython: 0.29.2
numpy: 1.16.0
scipy: 1.2.1
pyarrow: 0.12.0
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.7.5
pytz: 2018.9
blosc: None
bottleneck: None
tables: 3.4.4
numexpr: 2.6.9
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: None
html5lib: None
sqlalchemy: 1.2.16
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Closing CandidateMay be closeable, needs more eyeballsDocsDtype ConversionsUnexpected or buggy dtype conversionsNuisance ColumnsIdentifying/Dropping nuisance columns in reductions, groupby.add, DataFrame.applyReduction Operationssum, mean, min, max, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions