Skip to content

Commit 09ea608

Browse files
committed
Merge pull request #9209 from mortada/numeric_only
added mising numeric_only option for DataFrame.std/var/sem
2 parents 5453644 + cd4c3ea commit 09ea608

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

doc/source/whatsnew/v0.16.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -620,3 +620,5 @@ Bug Fixes
620620
- Fixed bug with reading CSV files from Amazon S3 on python 3 raising a TypeError (:issue:`9452`)
621621
- Bug in the Google BigQuery reader where the 'jobComplete' key may be present but False in the query results (:issue:`8728`)
622622
- Bug in ``Series.values_counts`` with excluding ``NaN`` for categorical type ``Series`` with ``dropna=True`` (:issue:`9443`)
623+
624+
- Fixed mising numeric_only option for ``DataFrame.std/var/sem`` (:issue:`9201`)

pandas/core/generic.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4093,7 +4093,7 @@ def _make_stat_function_ddof(name, desc, f):
40934093
@Substitution(outname=name, desc=desc)
40944094
@Appender(_num_doc)
40954095
def stat_func(self, axis=None, skipna=None, level=None, ddof=1,
4096-
**kwargs):
4096+
numeric_only=None, **kwargs):
40974097
if skipna is None:
40984098
skipna = True
40994099
if axis is None:
@@ -4102,6 +4102,7 @@ def stat_func(self, axis=None, skipna=None, level=None, ddof=1,
41024102
return self._agg_by_level(name, axis=axis, level=level,
41034103
skipna=skipna, ddof=ddof)
41044104
return self._reduce(f, name, axis=axis,
4105+
numeric_only=numeric_only,
41054106
skipna=skipna, ddof=ddof)
41064107
stat_func.__name__ = name
41074108
return stat_func

pandas/core/nanops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def _get_counts_nanvar(mask, axis, ddof):
332332
def _nanvar(values, axis=None, skipna=True, ddof=1):
333333
# private nanvar calculator
334334
mask = isnull(values)
335-
if not is_floating_dtype(values):
335+
if is_any_int_dtype(values):
336336
values = values.astype('f8')
337337

338338
count, d = _get_counts_nanvar(mask, axis, ddof)

pandas/tests/test_frame.py

+26
Original file line numberDiff line numberDiff line change
@@ -11503,6 +11503,32 @@ def test_var_std(self):
1150311503
self.assertFalse((result < 0).any())
1150411504
nanops._USE_BOTTLENECK = True
1150511505

11506+
def test_numeric_only_flag(self):
11507+
# GH #9201
11508+
methods = ['sem', 'var', 'std']
11509+
df1 = DataFrame(np.random.randn(5, 3), columns=['foo', 'bar', 'baz'])
11510+
# set one entry to a number in str format
11511+
df1.ix[0, 'foo'] = '100'
11512+
11513+
df2 = DataFrame(np.random.randn(5, 3), columns=['foo', 'bar', 'baz'])
11514+
# set one entry to a non-number str
11515+
df2.ix[0, 'foo'] = 'a'
11516+
11517+
for meth in methods:
11518+
result = getattr(df1, meth)(axis=1, numeric_only=True)
11519+
expected = getattr(df1[['bar', 'baz']], meth)(axis=1)
11520+
assert_series_equal(expected, result)
11521+
11522+
result = getattr(df2, meth)(axis=1, numeric_only=True)
11523+
expected = getattr(df2[['bar', 'baz']], meth)(axis=1)
11524+
assert_series_equal(expected, result)
11525+
11526+
assertRaisesRegexp(TypeError, 'float',
11527+
getattr(df1, meth), axis=1, numeric_only=False)
11528+
11529+
assertRaisesRegexp(TypeError, 'float',
11530+
getattr(df2, meth), axis=1, numeric_only=False)
11531+
1150611532
def test_sem(self):
1150711533
alt = lambda x: np.std(x, ddof=1)/np.sqrt(len(x))
1150811534
self._check_stat_op('sem', alt)

0 commit comments

Comments
 (0)