8000 MAINT: let average preserve subclass information. · numpy/numpy@3f3d205 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3f3d205

Browse files
committed
MAINT: let average preserve subclass information.
This behaviour matches that for most other numpy functions (such as np.mean). It was initially slated for 1.12, but replaced by a FutureWarning. Hence, this is for 1.13.
1 parent ec02bdc commit 3f3d205

File tree

3 files changed

+15
-18
lines changed

3 files changed

+15
-18
lines changed

doc/release/1.13.0-notes.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ DeprecationWarning to error
3333
FutureWarning to changed behavior
3434
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3535

36+
* ``numpy.average`` now preserves subclasses, matching the behavior of most
37+
other numpy functions such as ``mean``. As a consequence, also calls that
38+
returned a scalar may now return a subclass array scalar.
39+
40+
3641
C API
3742
~~~~~
3843

@@ -53,3 +58,10 @@ Improvements
5358

5459
Changes
5560
=======
61+
62+
``average`` now preserves subclasses
63+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
64+
For ndarray subclasses, ``numpy.average`` will now return an instance of the
65+
subclass, matching the behavior of most other numpy functions such as ``mean``.
66+
As a consequence, also calls that returned a scalar may now return a subclass
67+
array scalar.

numpy/lib/function_base.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,19 +1092,7 @@ def average(a, axis=None, weights=None, returned=False):
10921092
TypeError: Axis must be specified when shapes of a and weights differ.
10931093
10941094
"""
1095-
# 3/19/2016 1.12.0:
1096-
# replace the next few lines with "a = np.asanyarray(a)"
1097-
if (type(a) not in (np.ndarray, np.matrix) and
1098-
issubclass(type(a), np.ndarray)):
1099-
warnings.warn("np.average currently does not preserve subclasses, but "
1100-
"will do so in the future to match the behavior of most "
1101-
"other numpy functions such as np.mean. In particular, "
1102-
"this means calls which returned a scalar may return a "
1103-
"0-d subclass object instead.",
1104-
FutureWarning, stacklevel=2)
1105-
1106-
if not isinstance(a, np.matrix):
1107-
a = np.asarray(a)
1095+
a = np.asanyarray(a)
11081096

11091097
if weights is None:
11101098
avg = a.mean(axis)

numpy/lib/tests/test_function_base.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,8 @@ class subclass(np.ndarray):
321321
a = np.array([[1,2],[3,4]]).view(subclass)
322322
w = np.array([[1,2],[3,4]]).view(subclass)
323323

324-
with suppress_warnings() as sup:
325-
# Note that the warning is spurious, because the test checks
326-
# for weights while a is ignored.
327-
sup.filter(FutureWarning, "np.average currently does not preserve")
328-
assert_equal(type(np.average(a, weights=w)), subclass)
324+
assert_equal(type(np.average(a)), subclass)
325+
assert_equal(type(np.average(a, weights=w)), subclass)
329326

330327
# also test matrices
331328
a = np.matrix([[1,2],[3,4]])

0 commit comments

Comments
 (0)
0