8000 MAINT: let average preserve subclass information. by mhvk · Pull Request #8290 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: let average preserve subclass information. #8290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file 8000
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc/release/1.13.0-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ DeprecationWarning to error
FutureWarning to changed behavior
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

* ``numpy.average`` now preserves subclasses, matching the behavior of most
other numpy functions such as ``mean``. As a consequence, also calls that
returned a scalar may now return a subclass array scalar.


C API
~~~~~

Expand All @@ -53,3 +58,10 @@ Improvements

Changes
=======

``average`` now preserves subclasses
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8000 For ndarray subclasses, ``numpy.average`` will now return an instance of the
subclass, matching the behavior of most other numpy functions such as ``mean``.
As a consequence, also calls that returned a scalar may now return a subclass
array scalar.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add something under Compatibility notes? If you look In the 1.12 notes there is a subsection FutureWarning to changed behavior that has a list of changes.

14 changes: 1 addition & 13 deletions numpy/lib/function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,19 +1092,7 @@ def average(a, axis=None, weights=None, returned=False):
TypeError: Axis must be specified when shapes of a and weights differ.

"""
# 3/19/2016 1.12.0:
# replace the next few lines with "a = np.asanyarray(a)"
if (type(a) not in (np.ndarray, np.matrix) and
issubclass(type(a), np.ndarray)):
warnings.warn("np.average currently does not preserve subclasses, but "
"will do so in the future to match the behavior of most "
"other numpy functions such as np.mean. In particular, "
"this means calls which returned a scalar may return a "
"0-d subclass object instead.",
FutureWarning, stacklevel=2)

if not isinstance(a, np.matrix):
a = np.asarray(a)
a = np.asanyarray(a)

if weights is None:
avg = a.mean(axis)
Expand Down
7 changes: 2 additions & 5 deletions numpy/lib/tests/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,8 @@ class subclass(np.ndarray):
a = np.array([[1,2],[3,4]]).view(subclass)
w = np.array([[1,2],[3,4]]).view(subclass)

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

# also test matrices
a = np.matrix([[1,2],[3,4]])
Expand Down
0