Skip to content

Commit a4a4008

Browse files
committed
BUG: mean overflows for integer dtypes (fixes pandas-dev#10155)
1 parent 9b4d154 commit a4a4008

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pandas/core/nanops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def nansum(values, axis=None, skipna=True):
254254
@bottleneck_switch()
255255
def nanmean(values, axis=None, skipna=True):
256256
values, mask, dtype, dtype_max = _get_values(values, skipna, 0)
257-
the_sum = _ensure_numeric(values.sum(axis, dtype=dtype_max))
257+
the_sum = _ensure_numeric(values.sum(axis, dtype=np.float64))
258258
count = _get_counts(mask, axis)
259259

260260
if axis is not None and getattr(the_sum, 'ndim', False):

pandas/tests/test_nanops.py

+11
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,17 @@ def test_nanmean(self):
323323
allow_complex=False, allow_obj=False,
324324
allow_str=False, allow_date=False, allow_tdelta=True)
325325

326+
# GH 10155
327+
# In the previous implementation mean can overflow for int dtypes, it
328+
# is now consistent with numpy
329+
from pandas import Series
330+
for a in [2 ** 55, -2 ** 55, 20150515061816532]:
331+
s = Series(a, index=range(500))
332+
result = s.mean()
333+
np_result = s.values.mean()
334+
self.assertEqual(result, a)
335+
self.assertEqual(result, np_result)
336+
326337
def test_nanmedian(self):
327338
self.check_funs(nanops.nanmedian, np.median,
328339
allow_complex=False, allow_str=False, allow_date=False,

0 commit comments

Comments
 (0)