diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2017-03-08 11:07:06 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-08 11:07:06 +0000 |
commit | 485b099cd4b82d65dc38cb2b28c7119f003c76c4 (patch) | |
tree | 809cbc5926d6c38aa87e42f980a4e9b5568901c0 /numpy/lib | |
parent | 6a3edf3210b439a4d1a51acb4e01bac017697ee6 (diff) | |
parent | 1588ae39ffb51ea916f03510671aab711fdfb568 (diff) | |
download | python-numpy-485b099cd4b82d65dc38cb2b28c7119f003c76c4.tar.gz python-numpy-485b099cd4b82d65dc38cb2b28c7119f003c76c4.tar.bz2 python-numpy-485b099cd4b82d65dc38cb2b28c7119f003c76c4.zip |
Merge pull request #8750 from warut-vijit/master
BUG: Fix np.average for object arrays
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/function_base.py | 2 | ||||
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 7 |
2 files changed, 8 insertions, 1 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index c54512c21..0903790bd 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -1135,7 +1135,7 @@ def average(a, axis=None, weights=None, returned=False): wgt = wgt.swapaxes(-1, axis) scl = wgt.sum(axis=axis, dtype=result_dtype) - if (scl == 0.0).any(): + if np.any(scl == 0.0): raise ZeroDivisionError( "Weights sum to zero, can't be normalized") diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 4fb0dba51..188c1c2ea 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -3,6 +3,7 @@ from __future__ import division, absolute_import, print_function import operator import warnings import sys +import decimal import numpy as np from numpy.testing import ( @@ -341,6 +342,12 @@ class TestAverage(TestCase): w = np.array([[1,2],[3,4]], dtype=wt) assert_equal(np.average(a, weights=w).dtype, np.dtype(rt)) + def test_object_dtype(self): + a = np.array([decimal.Decimal(x) for x in range(10)]) + w = np.array([decimal.Decimal(1) for _ in range(10)]) + w /= w.sum() + assert_almost_equal(a.mean(0), average(a, weights=w)) + class TestSelect(TestCase): choices = [np.array([1, 2, 3]), np.array([4, 5, 6]), |