From 2a71609af953dd69fa6d17e1a2176ca8b4533701 Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Mon, 24 Apr 2017 22:07:26 -0400 Subject: [PATCH 1/2] REGR: bug in moments when using bottleneck closes #16116 --- pandas/core/nanops.py | 1 + pandas/tests/frame/test_analytics.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index 5ce302967de24..32d9ca791ba08 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -381,6 +381,7 @@ def nanstd(values, axis=None, skipna=True, ddof=1): @bottleneck_switch(ddof=1) def nanvar(values, axis=None, skipna=True, ddof=1): + values = _values_from_object(values) dtype = values.dtype mask = isnull(values) if is_any_int_dtype(values): diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py index cd98460d8609c..ad82bab02ec0c 100644 --- a/pandas/tests/frame/test_analytics.py +++ b/pandas/tests/frame/test_analytics.py @@ -591,6 +591,22 @@ def test_numeric_only_flag(self): pytest.raises(TypeError, lambda: getattr(df2, meth)( axis=1, numeric_only=False)) + def test_mixed_ops(self): + # GH 16116 + df = DataFrame({'int': [1, 2, 3, 4], + 'float': [1., 2., 3., 4.], + 'str': ['a', 'b', 'c', 'd']}) + + for op in ['mean', 'std']: + result = getattr(df, op)() + assert len(result) == 2 + + if nanops._USE_BOTTLENECK: + nanops._USE_BOTTLENECK = False + result = getattr(df, op)() + assert len(result) == 2 + nanops._USE_BOTTLENECK = True + def test_cumsum(self): self.tsframe.loc[5:10, 0] = nan self.tsframe.loc[10:15, 1] = nan From 15afc097113b794693391f290d70792871a8e050 Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Mon, 24 Apr 2017 22:21:03 -0400 Subject: [PATCH 2/2] add fixes for kurt/skew --- pandas/core/nanops.py | 2 ++ pandas/tests/frame/test_analytics.py | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index 32d9ca791ba08..e9be43b184537 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -490,6 +490,7 @@ def nanskew(values, axis=None, skipna=True): """ + values = _values_from_object(values) mask = isnull(values) if not is_float_dtype(values.dtype): values = values.astype('f8') @@ -544,6 +545,7 @@ def nankurt(values, axis=None, skipna=True): central moment. """ + values = _values_from_object(values) mask = isnull(values) if not is_float_dtype(values.dtype): values = values.astype('f8') diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py index ad82bab02ec0c..0941f0af6bec5 100644 --- a/pandas/tests/frame/test_analytics.py +++ b/pandas/tests/frame/test_analytics.py @@ -597,7 +597,8 @@ def test_mixed_ops(self): 'float': [1., 2., 3., 4.], 'str': ['a', 'b', 'c', 'd']}) - for op in ['mean', 'std']: + for op in ['mean', 'std', 'var', 'skew', + 'kurt', 'sem']: result = getattr(df, op)() assert len(result) == 2