Repository: pandas-dev/pandas

Issue:
performance regression in DataFrame.sum?
```
In [29]: df = DataFrame(randint(2, size=(8e6, 10)))

In [30]: time df.sum()
CPU times: user 1.21 s, sys: 0.03 s, total: 1.24 s
Wall time: 1.23 s
Out[30]:
0    3999325
1    3998264
2    4000047
3    3997902
4    4001078
5    4001965
6    4001701
7    4000482
8    3997581
9    4000691
dtype: int64

In [31]: time df.mean()
CPU times: user 0.38 s, sys: 0.00 s, total: 0.38 s
Wall time: 0.39 s
Out[31]:
0    0.5
1    0.5
2    0.5
3    0.5
4    0.5
5    0.5
6    0.5
7    0.5
8    0.5
9    0.5
dtype: float64
```

Should sum be 3.15x slower than mean?



Discussion:
```
INSTALLED VERSIONS
------------------
Python: 2.7.5.final.0
OS: Linux 3.9.9-1-ARCH #1 SMP PREEMPT Wed Jul 3 22:45:16 CEST 2013 x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

Cython: 0.19.1
Numpy: 1.7.1
Scipy: 0.12.0
statsmodels: 0.5.0.dev-db14222
    patsy: 0.1.0
scikits.timeseries: 0.91.3
dateutil: 2.1
pytz: 2013b
bottleneck: 0.6.0
PyTables: 3.0.0
    numexpr: 2.1
matplotlib: 1.2.1
openpyxl: 1.6.2
xlrd: 0.9.2
xlwt: 0.7.5
sqlalchemy: 0.8.1
lxml: 3.2.1
bs4: 4.2.1
html5lib: 1.0b1
```

issue is slightly worse before https://github.com/pydata/pandas/issues/3731 so that's not the cause 

Only happens for `int64` dtypes so far...perf is on point for `float64`


Resolving patch:
diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py
--- a/pandas/core/nanops.py
+++ b/pandas/core/nanops.py
@@ -71,7 +71,9 @@ def f(values, axis=None, skipna=True, **kwds):
 
                 if _USE_BOTTLENECK and skipna and _bn_ok_dtype(values.dtype):
                     result = bn_func(values, axis=axis, **kwds)
-                    # prefer to treat inf/-inf as NA
+
+                    # prefer to treat inf/-inf as NA, but must compute the func
+                    # twice :(
                     if _has_infs(result):
                         result = alt(values, axis=axis, skipna=skipna, **kwds)
                 else:
@@ -86,7 +88,8 @@ def f(values, axis=None, skipna=True, **kwds):
 
 def _bn_ok_dtype(dt):
     # Bottleneck chokes on datetime64
-    return dt != np.object_ and not issubclass(dt.type, (np.datetime64,np.timedelta64))
+    time_types = np.datetime64, np.timedelta64
+    return dt != np.object_ and not issubclass(dt.type, time_types)
 
 
 def _has_infs(result):
@@ -95,10 +98,8 @@ def _has_infs(result):
             return lib.has_infs_f8(result)
         elif result.dtype == 'f4':
             return lib.has_infs_f4(result)
-        else:  # pragma: no cover
-            raise TypeError('Only suppose float32/64 here')
-    else:
-        return np.isinf(result) or np.isneginf(result)
+        return False
+    return np.isinf(result) or np.isneginf(result)
 
 def _get_fill_value(dtype, fill_value=None, fill_value_typ=None):
     """ return the correct fill value for the dtype of the values """
diff --git a/vb_suite/stat_ops.py b/vb_suite/stat_ops.py
--- a/vb_suite/stat_ops.py
+++ b/vb_suite/stat_ops.py
@@ -43,6 +43,35 @@
     Benchmark("df[1].sum(level=[0, 1])", setup, repeat=1,
               start_date=datetime(2011, 11, 15))
 
+sum_setup = common_setup + """
+df = DataFrame(np.random.randn(100000, 4))
+dfi = DataFrame(np.random.randint(1000, size=df.shape))
+"""
+
+stat_ops_frame_sum_int_axis_0 = \
+    Benchmark("dfi.sum()", sum_setup, start_date=datetime(2013, 7, 25))
+
+stat_ops_frame_sum_float_axis_0 = \
+    Benchmark("df.sum()", sum_setup, start_date=datetime(2013, 7, 25))
+
+stat_ops_frame_mean_int_axis_0 = \
+    Benchmark("dfi.mean()", sum_setup, start_date=datetime(2013, 7, 25))
+
+stat_ops_frame_mean_float_axis_0 = \
+    Benchmark("df.mean()", sum_setup, start_date=datetime(2013, 7, 25))
+
+stat_ops_frame_sum_int_axis_1 = \
+    Benchmark("dfi.sum(1)", sum_setup, start_date=datetime(2013, 7, 25))
+
+stat_ops_frame_sum_float_axis_1 = \
+    Benchmark("df.sum(1)", sum_setup, start_date=datetime(2013, 7, 25))
+
+stat_ops_frame_mean_int_axis_1 = \
+    Benchmark("dfi.mean(1)", sum_setup, start_date=datetime(2013, 7, 25))
+
+stat_ops_frame_mean_float_axis_1 = \
+    Benchmark("df.mean(1)", sum_setup, start_date=datetime(2013, 7, 25))
+
 #----------------------------------------------------------------------
 # rank
 


Test patch: