|
5 | 5 |
|
6 | 6 | import numpy as np
|
7 | 7 |
|
8 |
| -from pandas.core.common import isnull |
| 8 | +from pandas.core.common import isnull, is_integer_dtype |
9 | 9 | import pandas.core.nanops as nanops
|
10 | 10 | import pandas.util.testing as tm
|
11 | 11 |
|
@@ -323,6 +323,28 @@ def test_nanmean(self):
|
323 | 323 | allow_complex=False, allow_obj=False,
|
324 | 324 | allow_str=False, allow_date=False, allow_tdelta=True)
|
325 | 325 |
|
| 326 | + def test_nanmean_overflow(self): |
| 327 | + # GH 10155 |
| 328 | + # In the previous implementation mean can overflow for int dtypes, it |
| 329 | + # is now consistent with numpy |
| 330 | + from pandas import Series |
| 331 | + for a in [2 ** 55, -2 ** 55, 20150515061816532]: |
| 332 | + s = Series(a, index=range(10), dtype=np.int64) |
| 333 | + result = s.mean() |
| 334 | + np_result = s.values.mean() |
| 335 | + self.assertEqual(result, a) |
| 336 | + self.assertEqual(result, np_result) |
| 337 | + self.assertTrue(result.dtype == np.float64) |
| 338 | + |
| 339 | + # check returned dtype |
| 340 | + for dtype in [np.int16, np.int32, np.int64, np.float16, np.float32, np.float64]: |
| 341 | + s = Series(range(10), dtype=dtype) |
| 342 | + result = s.mean() |
| 343 | + if is_integer_dtype(dtype): |
| 344 | + self.assertTrue(result.dtype == np.float64) |
| 345 | + else: |
| 346 | + self.assertTrue(result.dtype == dtype) |
| 347 | + |
326 | 348 | def test_nanmedian(self):
|
327 | 349 | self.check_funs(nanops.nanmedian, np.median,
|
328 | 350 | allow_complex=False, allow_str=False, allow_date=False,
|
|
0 commit comments