8000 API: Cleaning `numpy/__init__.py` and main namespace - Part 4 [NEP 52] by mtsokol · Pull Request #24445 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

API: Cleaning numpy/__init__.py and main namespace - Part 4 [NEP 52] #24445

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 9 commits into from
Sep 5, 2023
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
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/release/upcoming_changes/24445.deprecation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
* ``np.trapz`` has been deprecated. Use ``scipy.interpolate.trapezoid`` instead.

* ``np.in1d`` has been deprecated. Use ``np.isin`` instead.

* Alias ``np.row_stack`` has been deprecated. Use ``np.vstack`` directly.
1 change: 0 additions & 1 deletion doc/source/reference/routines.array-manipulation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ Joining arrays
hstack
dstack
column_stack
row_stack

Splitting arrays
================
Expand Down
1 change: 0 additions & 1 deletion doc/source/reference/routines.ma.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ Changing the number of dimensions
ma.hstack
ma.hsplit
ma.mr_
ma.row_stack
ma.vstack


Expand Down
1 change: 0 additions & 1 deletion doc/source/reference/routines.math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ Sums, products, differences
ediff1d
gradient
cross
trapz

Exponents and logarithms
------------------------
Expand Down
8 changes: 0 additions & 8 deletions doc/source/user/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -767,14 +767,6 @@ It is equivalent to `hstack` only for 2D arrays::
array([[4., 3.],
[2., 8.]])

On the other hand, the function `row_stack` is equivalent to `vstack`
for any input arrays. In fact, `row_stack` is an alias for `vstack`::

>>> np.column_stack is np.hstack
False
>>> np.row_stack is np.vstack
True

In general, for arrays with more than two dimensions,
`hstack` stacks along their second
axes, `vstack` stacks along their
Expand Down
3 changes: 0 additions & 3 deletions numpy/core/fromnumeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -2241,8 +2241,6 @@ def sum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue,

cumsum : Cumulative sum of array elements.

trapz : Integration of array values using the composite trapezoidal rule.

mean, average

Notes
Expand Down Expand Up @@ -2544,7 +2542,6 @@ def cumsum(a, axis=None, dtype=None, out=None):
See Also
--------
sum : Sum array elements.
trapz : Integration of array values using the composite trapezoidal rule.
diff : Calculate the n-th discrete difference along given axis.

Notes
Expand Down
2 changes: 0 additions & 2 deletions numpy/core/shape_base.py
A3DB
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,6 @@ def vstack(tup, *, dtype=None, casting="same_kind"):
and r/g/b channels (third axis). The functions `concatenate`, `stack` and
`block` provide more general stacking and concatenation operations.

``np.row_stack`` is an alias for `vstack`. They are the same function.

Parameters
----------
tup : sequence of ndarrays
Expand Down
5 changes: 5 additions & 0 deletions numpy/core/tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,7 @@ def test_lib_functions_deprecation_call(self):
from numpy.lib._shape_base_impl import get_array_wrap
from numpy.core.numerictypes import maximum_sctype
from numpy.lib.tests.test_io import TextIO
from numpy import in1d, row_stack, trapz

self.assert_deprecated(lambda: safe_eval("None"))

Expand All @@ -766,3 +767,7 @@ def test_lib_functions_deprecation_call(self):
self.assert_deprecated(lambda: disp("test"))
self.assert_deprecated(lambda: get_array_wrap())
self.assert_deprecated(lambda: maximum_sctype(int))

self.assert_deprecated(lambda: in1d([1], [1]))
self.assert_deprecated(lambda: row_stack([[]]))
self.assert_deprecated(lambda: trapz([1], [1]))
21 changes: 0 additions & 21 deletions numpy/core/tests/test_overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,24 +737,3 @@ def __array__(self):
bound = np.mean.__get__(MyClass) # classmethod
with pytest.raises(TypeError, match="unsupported operand type"):
bound()


def test_scipy_trapz_support_shim():
# SciPy 1.10 and earlier "clone" trapz in this way, so we have a
# support shim in place: https://github.com/scipy/scipy/issues/17811
# That should be removed eventually. This test copies what SciPy does.
# Hopefully removable 1 year after SciPy 1.11; shim added to NumPy 1.25.
import types
import functools

def _copy_func(f):
# Based on http://stackoverflow.com/a/6528148/190597 (Glenn Maynard)
g = types.FunctionType(f.__code__, f.__globals__, name=f.__name__,
argdefs=f.__defaults__, closure=f.__closure__)
g = functools.update_wrapper(g, f)
g.__kwdefaults__ = f.__kwdefaults__
return g

trapezoid = _copy_func(np.trapz)

assert np.trapz([1, 2]) == trapezoid([1, 2])
28 changes: 19 additions & 9 deletions numpy/lib/_arraysetops_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

"""
import functools
import warnings

import numpy as np
from numpy.core import overrides
Expand Down Expand Up @@ -518,11 +519,12 @@ def in1d(ar1, ar2, assume_unique=False, invert=False, *, kind=None):
"""
Test whether each element of a 1-D array is also present in a second array.

.. deprecated:: 2.0
Use :func:`isin` instead of `in1d` for new code.

Returns a boolean array the same length as `ar1` that is True
where an element of `ar1` is in `ar2` and False otherwise.

We recommend using :func:`isin` instead of `in1d` for new code.

Parameters
----------
ar1 : (M,) array_like
Expand Down Expand Up @@ -604,6 +606,18 @@ def in1d(ar1, ar2, assume_unique=False, invert=False, *, kind=None):
>>> test[mask]
array([1, 5])
"""

# Deprecated in NumPy 2.0, 2023-08-18
warnings.warn(
"`in1d` is deprecated. Use `np.isin` instead.",
DeprecationWarning,
stacklevel=2
)

return _in1d(ar1, ar2, assume_unique, invert, kind=kind)


def _in1d(ar1, ar2, assume_unique=False, invert=False, *, kind=None):
# Ravel both arrays, behavior for the first array could be different
ar1 = np.asarray(ar1).ravel()
ar2 = np.asarray(ar2).ravel()
Expand Down Expand Up @@ -805,10 +819,6 @@ def isin(element, test_elements, assume_unique=False, invert=False, *,
Has the same shape as `element`. The values `element[isin]`
are in `test_elements`.

See Also
--------
in1d : Flattened version of this function.

Notes
-----

Expand Down Expand Up @@ -876,8 +886,8 @@ def isin(element, test_elements, assume_unique=False, invert=False, *,
[ True, False]])
"""
element = np.asarray(element)
return in1d(element, test_elements, assume_unique=assume_unique,
invert=invert, kind=kind).reshape(element.shape)
return _in1d(element, test_elements, assume_unique=assume_unique,
invert=invert, kind=kind).reshape(element.shape)


def _union1d_dispatcher(ar1, ar2):
Expand Down Expand Up @@ -957,4 +967,4 @@ def setdiff1d(ar1, ar2, assume_unique=False):
else:
ar1 = unique(ar1)
ar2 = unique(ar2)
return ar1[in1d(ar1, ar2, assume_unique=True, invert=True)]
return ar1[_in1d(ar1, ar2, assume_unique=True, invert=True)]
11 changes: 11 additions & 0 deletions numpy/lib/_function_base_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -4783,6 +4783,9 @@ def trapz(y, x=None, dx=1.0, axis=-1):
r"""
Integrate along the given axis using the composite trapezoidal rule.

.. deprecated:: 2.0
Use `scipy.integrate.trapezoid` instead.

If `x` is provided, the integration happens in sequence along its
elements - they are not sorted.

Expand Down Expand Up @@ -4880,6 +4883,14 @@ def trapz(y, x=None, dx=1.0, axis=-1):
>>> np.trapz(a, axis=1)
array([2., 8.])
"""

# Deprecated in NumPy 2.0, 2023-08-18
warnings.warn(
Copy link
Contributor

Choose a reason for hiding this comment

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

This will affect a lot of analysis code... not just developers, but users.

Copy link
Member

Choose a reason for hiding this comment

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

FWIW, we did that same rename in scipy.integrate a few years ago, and got no complaints. This name is considered a slur, and that's an extra reason to get rid of it. We touched on this in the 2.0 dev call IIRC, and folks were in favor of names that are both more descriptive and more inclusive (cumsum & co too).

It's deprecated rather than outright removed here exactly because it's used quite a bit. So no code will actually break in the 2.0 release.

Copy link
Contributor

Choose a reason for hiding this comment

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

Huh, didn't know it's a slur. Thanks for the clarification!

Copy link
Member
@rgommers rgommers Sep 7, 2023

Choose a reason for hiding this comment

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

Neither did I until scipy/scipy#12924.

"`trapz` is deprecated. Use `scipy.integrate.trapezoid` instead.",
DeprecationWarning,
stacklevel=2
)

y = asanyarray(y)
if x is None:
d = dx
Expand Down
14 changes: 13 additions & 1 deletion numpy/lib/_shape_base_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,19 @@ def expand_dims(a, axis):
return a.reshape(shape)


row_stack = vstack
# TODO: Remove once deprecation period passes
def row_stack(tup, *, dtype=None, casting="same_kind"):
# Deprecated in NumPy 2.0, 2023-08-18
warnings.warn(
"`row_stack` alias is deprecated. "
"Use `np.vstack` directly.",
DeprecationWarning,
stacklevel=2
)
return vstack(tup, dtype=dtype, casting=casting)


row_stack.__doc__ = vstack.__doc__


def _column_stack_dispatcher(tup):
Expand Down
Loading
0