Skip to content

BUG: fix sum over integer frames #4366

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
Jul 26, 2013
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/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pandas 0.13
representation of the index (:issue:`4136`)
- Fix running of stata IO tests. Now uses temporary files to write
(:issue:`4353`)
- Fixed an issue where ``DataFrame.sum`` was slower than ``DataFrame.mean``
for integer valued frames (:issue:`4365`)

pandas 0.12
===========
Expand Down
3 changes: 3 additions & 0 deletions doc/source/v0.13.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ Bug Fixes
- Fix running of stata IO tests. Now uses temporary files to write
(:issue:`4353`)

- Fixed an issue where ``DataFrame.sum`` was slower than ``DataFrame.mean``
for integer valued frames (:issue:`4365`)

See the :ref:`full release notes
<release>` or issue tracker
on GitHub for a complete list.
13 changes: 7 additions & 6 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand All @@ -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 """
Expand Down
29 changes: 29 additions & 0 deletions vb_suite/stat_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down