Skip to content

Commit 35e072c

Browse files
committed
ENH: Deprecate in1d, trapz, row_stack [skip ci]
1 parent 79b434c commit 35e072c

17 files changed

+130
-208
lines changed

doc/source/reference/routines.array-manipulation.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ Joining arrays
7272
hstack
7373
dstack
7474
column_stack
75-
row_stack
7675

7776
Splitting arrays
7877
================

doc/source/reference/routines.ma.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ Changing the number of dimensions
145145
ma.hstack
146146
ma.hsplit
147147
ma.mr_
148-
ma.row_stack
149148
ma.vstack
150149

151150

doc/source/reference/routines.math.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ Sums, products, differences
6464
ediff1d
6565
gradient
6666
cross
67-
trapz
6867

6968
Exponents and logarithms
7069
------------------------

doc/source/user/quickstart.rst

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -767,14 +767,6 @@ It is equivalent to `hstack` only for 2D arrays::
767767
array([[4., 3.],
768768
[2., 8.]])
769769

770-
On the other hand, the function `row_stack` is equivalent to `vstack`
771-
for any input arrays. In fact, `row_stack` is an alias for `vstack`::
772-
773-
>>> np.column_stack is np.hstack
774-
False
775-
>>> np.row_stack is np.vstack
776-
True
777-
778770
In general, for arrays with more than two dimensions,
779771
`hstack` stacks along their second
780772
axes, `vstack` stacks along their

numpy/core/fromnumeric.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,8 +2241,6 @@ def sum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue,
22412241
22422242
cumsum : Cumulative sum of array elements.
22432243
2244-
trapz : Integration of array values using the composite trapezoidal rule.
2245-
22462244
mean, average
22472245
22482246
Notes
@@ -2544,7 +2542,6 @@ def cumsum(a, axis=None, dtype=None, out=None):
25442542
See Also
25452543
--------
25462544
sum : Sum array elements.
2547-
trapz : Integration of array values using the composite trapezoidal rule.
25482545
diff : Calculate the n-th discrete difference along given axis.
25492546
25502547
Notes

numpy/core/shape_base.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,6 @@ def vstack(tup, *, dtype=None, casting="same_kind"):
230230
and r/g/b channels (third axis). The functions `concatenate`, `stack` and
231231
`block` provide more general stacking and concatenation operations.
232232
233-
``np.row_stack`` is an alias for `vstack`. They are the same function.
234-
235233
Parameters
236234
----------
237235
tup : sequence of ndarrays

numpy/core/tests/test_deprecations.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,7 @@ def test_lib_functions_deprecation_call(self):
759759
from numpy.lib.shape_base import get_array_wrap
760760
from numpy.core.numerictypes import maximum_sctype
761761
from numpy.lib.tests.test_io import TextIO
762+
from numpy import in1d, row_stack, trapz
762763

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

@@ -770,3 +771,7 @@ def test_lib_functions_deprecation_call(self):
770771
self.assert_deprecated(lambda: disp("test"))
771772
self.assert_deprecated(lambda: get_array_wrap())
772773
self.assert_deprecated(lambda: maximum_sctype(int))
774+
775+
self.assert_deprecated(lambda: in1d([1], [1]))
776+
self.assert_deprecated(lambda: row_stack([[]]))
777+
self.assert_deprecated(lambda: trapz([1], [1]))

numpy/core/tests/test_overrides.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -737,24 +737,3 @@ def __array__(self):
737737
bound = np.mean.__get__(MyClass) # classmethod
738738
with pytest.raises(TypeError, match="unsupported operand type"):
739739
bound()
740-
741-
742-
def test_scipy_trapz_support_shim():
743-
# SciPy 1.10 and earlier "clone" trapz in this way, so we have a
744-
# support shim in place: https://github.com/scipy/scipy/issues/17811
745-
# That should be removed eventually. This test copies what SciPy does.
746-
# Hopefully removable 1 year after SciPy 1.11; shim added to NumPy 1.25.
747-
import types
748-
import functools
749-
750-
def _copy_func(f):
751-
# Based on http://stackoverflow.com/a/6528148/190597 (Glenn Maynard)
752-
g = types.FunctionType(f.__code__, f.__globals__, name=f.__name__,
753-
argdefs=f.__defaults__, closure=f.__closure__)
754-
g = functools.update_wrapper(g, f)
755-
g.__kwdefaults__ = f.__kwdefaults__
756-
return g
757-
758-
trapezoid = _copy_func(np.trapz)
759-
760-
assert np.trapz([1, 2]) == trapezoid([1, 2])

numpy/lib/arraysetops.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
1616
"""
1717
import functools
18+
import warnings
1819

1920
import numpy as np
2021
from numpy.core import overrides
@@ -526,11 +527,12 @@ def in1d(ar1, ar2, assume_unique=False, invert=False, *, kind=None):
526527
"""
527528
Test whether each element of a 1-D array is also present in a second array.
528529
530+
.. deprecated:: 2.0
531+
Use :func:`isin` instead of `in1d` for new code.
532+
529533
Returns a boolean array the same length as `ar1` that is True
530534
where an element of `ar1` is in `ar2` and False otherwise.
531535
532-
We recommend using :func:`isin` instead of `in1d` for new code.
533-
534536
Parameters
535537
----------
536538
ar1 : (M,) array_like
@@ -614,6 +616,14 @@ def in1d(ar1, ar2, assume_unique=False, invert=False, *, kind=None):
614616
>>> test[mask]
615617
array([1, 5])
616618
"""
619+
620+
# Deprecated in NumPy 2.0, 2023-08-18
621+
warnings.warn(
622+
"`in1d` is deprecated. Use `np.isin` instead.",
623+
DeprecationWarning,
624+
stacklevel=2
625+
)
626+
617627
# Ravel both arrays, behavior for the first array could be different
618628
ar1 = np.asarray(ar1).ravel()
619629
ar2 = np.asarray(ar2).ravel()
@@ -817,7 +827,6 @@ def isin(element, test_elements, assume_unique=False, invert=False, *,
817827
818828
See Also
819829
--------
820-
in1d : Flattened version of this function.
821830
numpy.lib.arraysetops : Module with a number of other functions for
822831
performing set operations on arrays.
823832
@@ -887,8 +896,14 @@ def isin(element, test_elements, assume_unique=False, invert=False, *,
887896
[ True, False]])
888897
"""
889898
element = np.asarray(element)
890-
return in1d(element, test_elements, assume_unique=assume_unique,
891-
invert=invert, kind=kind).reshape(element.shape)
899+
900+
# TODO: remove warning catch once `in1d` deprecation period passes
901+
# and import it as a private method.
902+
with warnings.catch_warnings():
903+
warnings.simplefilter("ignore", DeprecationWarning)
904+
result = in1d(element, test_elements, assume_unique=assume_unique,
905+
invert=invert, kind=kind).reshape(element.shape)
906+
return result
892907

893908

894909
def _union1d_dispatcher(ar1, ar2):
@@ -978,4 +993,9 @@ def setdiff1d(ar1, ar2, assume_unique=False):
978993
else:
979994
ar1 = unique(ar1)
980995
ar2 = unique(ar2)
981-
return ar1[in1d(ar1, ar2, assume_unique=True, invert=True)]
996+
# TODO: remove warning catch once `in1d` deprecation period passes
997+
# and import it as a private method.
998+
with warnings.catch_warnings():
999+
warnings.simplefilter("ignore", DeprecationWarning)
1000+
result = ar1[in1d(ar1, ar2, assume_unique=True, invert=True)]
1001+
return result

numpy/lib/function_base.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4833,6 +4833,9 @@ def trapz(y, x=None, dx=1.0, axis=-1):
48334833
r"""
48344834
Integrate along the given axis using the composite trapezoidal rule.
48354835
4836+
.. deprecated:: 2.0
4837+
Use `scipy.integrate.trapezoid` instead.
4838+
48364839
If `x` is provided, the integration happens in sequence along its
48374840
elements - they are not sorted.
48384841
@@ -4930,6 +4933,14 @@ def trapz(y, x=None, dx=1.0, axis=-1):
49304933
>>> np.trapz(a, axis=1)
49314934
array([2., 8.])
49324935
"""
4936+
4937+
# Deprecated in NumPy 2.0, 2023-08-18
4938+
warnings.warn(
4939+
"`trapz` is deprecated. Use `scipy.integrate.trapezoid` instead.",
4940+
DeprecationWarning,
4941+
stacklevel=2
4942+
)
4943+
49334944
y = asanyarray(y)
49344945
if x is None:
49354946
d = dx

0 commit comments

Comments
 (0)