Skip to content

Commit 1cd562d

Browse files
committed
RFC: Rewrite util.array.searchsorted to return np.searchsorted(a, v, side='right')
Also raise `DeprecationWarning`
1 parent 83d3c6c commit 1cd562d

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

quantecon/util/array.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
99
"""
1010

11-
from numba import jit
11+
import warnings
12+
import numpy as np
13+
from numba import jit, objmode
1214

1315
# ----------------- #
1416
# -ARRAY UTILITIES- #
@@ -21,6 +23,10 @@ def searchsorted(a, v):
2123
that `a[i-1] <= v < a[i]` (for `i = 0`, `v < a[0]`); if `v[n-1] <=
2224
v`, return `n`, where `n = len(a)`.
2325
26+
.. deprecated::
27+
28+
Deprecated, use `np.searchsorted(a, v, side='right')` instead.
29+
2430
Parameters
2531
----------
2632
a : ndarray(float, ndim=1)
@@ -51,12 +57,11 @@ def searchsorted(a, v):
5157
3
5258
5359
"""
54-
lo = -1
55-
hi = len(a)
56-
while(lo < hi-1):
57-
m = (lo + hi) // 2
58-
if v < a[m]:
59-
hi = m
60-
else:
61-
lo = m
62-
return hi
60+
with objmode():
61+
warnings.warn(
62+
"`searchsorted(a, v)` is deprecated. "
63+
"Use `np.searchsorted(a, v, side='right')` instead.",
64+
DeprecationWarning,
65+
stacklevel=2,
66+
)
67+
return np.searchsorted(a, v, side='right')

quantecon/util/tests/test_array.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
"""
99
import numpy as np
1010
from numpy.testing import assert_
11+
import pytest
1112
from quantecon.util import searchsorted
1213

1314

15+
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
1416
def test_searchsorted():
1517
a = np.array([0.2, 0.4, 1.0])
1618
assert_(searchsorted(a, 0.1) == 0)
@@ -28,3 +30,9 @@ def test_searchsorted():
2830
a = np.ones(2)
2931
for (v, i) in zip([0, 1, 2], [0, 2, 2]):
3032
assert_(searchsorted(a, v) == i)
33+
34+
35+
def test_warns():
36+
a = np.array([0.2, 0.4, 1.0])
37+
with pytest.warns(DeprecationWarning):
38+
searchsorted(a, 0.5)

0 commit comments

Comments
 (0)