diff options
author | Pieter Eendebak <pieter.eendebak@gmail.com> | 2022-07-18 21:54:00 +0200 |
---|---|---|
committer | Pieter Eendebak <pieter.eendebak@gmail.com> | 2022-08-10 23:46:37 +0200 |
commit | 25d5e385ec537f911702f905ca050815e320865a (patch) | |
tree | ed42a135529a2a086ffcd7013fa9bb56f6b380a8 /numpy/lib/function_base.py | |
parent | 86a368f53ba15a610be4c6cb3a173446aef3aa80 (diff) | |
download | numpy-25d5e385ec537f911702f905ca050815e320865a.tar.gz |
BUG: fix np.average for Fraction elements
Diffstat (limited to 'numpy/lib/function_base.py')
-rw-r--r-- | numpy/lib/function_base.py | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index fb5dd6fdd..960285f7d 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -516,7 +516,8 @@ def average(a, axis=None, weights=None, returned=False, *, if weights is None: avg = a.mean(axis, **keepdims_kw) - scl = avg.dtype.type(a.size/avg.size) + avg_as_array = np.asanyarray(avg) + scl = avg_as_array.dtype.type(a.size/avg_as_array.size) else: wgt = np.asanyarray(weights) @@ -547,12 +548,12 @@ def average(a, axis=None, weights=None, returned=False, *, raise ZeroDivisionError( "Weights sum to zero, can't be normalized") - avg = np.multiply(a, wgt, + avg = avg_as_array = np.multiply(a, wgt, dtype=result_dtype).sum(axis, **keepdims_kw) / scl if returned: - if scl.shape != avg.shape: - scl = np.broadcast_to(scl, avg.shape).copy() + if scl.shape != avg_as_array.shape: + scl = np.broadcast_to(scl, avg_as_array.shape).copy() return avg, scl else: return avg |