Skip to content

added mising numeric_only option for DataFrame.std/var/sem #9209

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

Merged
merged 1 commit into from
Mar 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.16.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,5 @@ Bug Fixes
- Fixed bug with reading CSV files from Amazon S3 on python 3 raising a TypeError (:issue:`9452`)
- Bug in the Google BigQuery reader where the 'jobComplete' key may be present but False in the query results (:issue:`8728`)
- Bug in ``Series.values_counts`` with excluding ``NaN`` for categorical type ``Series`` with ``dropna=True`` (:issue:`9443`)

- Fixed mising numeric_only option for ``DataFrame.std/var/sem`` (:issue:`9201`)
3 changes: 2 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4090,7 +4090,7 @@ def _make_stat_function_ddof(name, desc, f):
@Substitution(outname=name, desc=desc)
@Appender(_num_doc)
def stat_func(self, axis=None, skipna=None, level=None, ddof=1,
**kwargs):
numeric_only=None, **kwargs):
if skipna is None:
skipna = True
if axis is None:
Expand All @@ -4099,6 +4099,7 @@ def stat_func(self, axis=None, skipna=None, level=None, ddof=1,
return self._agg_by_level(name, axis=axis, level=level,
skipna=skipna, ddof=ddof)
return self._reduce(f, name, axis=axis,
numeric_only=numeric_only,
skipna=skipna, ddof=ddof)
stat_func.__name__ = name
return stat_func
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ def _get_counts_nanvar(mask, axis, ddof):
def _nanvar(values, axis=None, skipna=True, ddof=1):
# private nanvar calculator
mask = isnull(values)
if not is_floating_dtype(values):
if is_any_int_dtype(values):
values = values.astype('f8')

count, d = _get_counts_nanvar(mask, axis, ddof)
Expand Down
26 changes: 26 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -11503,6 +11503,32 @@ def test_var_std(self):
self.assertFalse((result < 0).any())
nanops._USE_BOTTLENECK = True

def test_numeric_only_flag(self):
# GH #9201
methods = ['sem', 'var', 'std']
df1 = DataFrame(np.random.randn(5, 3), columns=['foo', 'bar', 'baz'])
# set one entry to a number in str format
df1.ix[0, 'foo'] = '100'

df2 = DataFrame(np.random.randn(5, 3), columns=['foo', 'bar', 'baz'])
# set one entry to a non-number str
df2.ix[0, 'foo'] = 'a'

for meth in methods:
result = getattr(df1, meth)(axis=1, numeric_only=True)
expected = getattr(df1[['bar', 'baz']], meth)(axis=1)
assert_series_equal(expected, result)

result = getattr(df2, meth)(axis=1, numeric_only=True)
expected = getattr(df2[['bar', 'baz']], meth)(axis=1)
assert_series_equal(expected, result)

assertRaisesRegexp(TypeError, 'float',
getattr(df1, meth), axis=1, numeric_only=False)

assertRaisesRegexp(TypeError, 'float',
getattr(df2, meth), axis=1, numeric_only=False)

def test_sem(self):
alt = lambda x: np.std(x, ddof=1)/np.sqrt(len(x))
self._check_stat_op('sem', alt)
Expand Down