<p dir="auto">For some inputs which have a step change, scipy.interpolate.interp1d returns incorrect values.</p>
<p dir="auto">The conditions for this to happen are:</p>
<ol dir="auto">
<li>The same x value should be present twice in the input with different y values, and</li>
<li>Some random property of the input: if it happens for a certain input, it will always happen, but it may not happen for a subset of the input which includes the problem values</li>
</ol>
<p dir="auto">The problem appears to be that the order of the y values as used is swapped versus what is in the input; similar to what might happen if the x and y values are sorted using an unstable sort (ie: non-order-preserving for equal inputs).</p>
<h4 dir="auto">Reproducing code example:</h4>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import numpy as np
import scipy.interpolate

x, y = (np.array([0.00000000e+00, 8.81834215e-05, 1.23456790e-03, 1.23456790e-03,
        2.29276896e-03, 2.29276896e-03, 2.55731922e-03, 2.55731922e-03,
        2.91005291e-03, 2.91005291e-03, 5.02645503e-03, 5.02645503e-03,
        5.11463845e-03, 5.11463845e-03, 2.40740741e-02, 2.40740741e-02,
        2.54850088e-02, 2.54850088e-02, 2.91005291e-02, 2.91005291e-02,
        3.08641975e-02, 3.08641975e-02, 5.41446208e-02, 5.41446208e-02,
        5.91710758e-02, 5.91710758e-02, 7.87477954e-02, 7.87477954e-02,
        9.88536155e-02, 9.88536155e-02, 1.16666667e-01, 1.16666667e-01,
        1.72927690e-01, 1.72927690e-01, 6.92151675e-01, 6.92151675e-01,
        8.38447972e-01, 8.38447972e-01, 1.00000000e+00]),
        np.array([0.        , 0.        , 0.        , 0.05555556, 0.05555556,
        0.11111111, 0.11111111, 0.16666667, 0.16666667, 0.22222222,
        0.22222222, 0.27777778, 0.27777778, 0.33333333, 0.33333333,
        0.38888889, 0.38888889, 0.44444444, 0.44444444, 0.5       ,
        0.5       , 0.55555556, 0.55555556, 0.61111111, 0.61111111,
        0.66666667, 0.66666667, 0.72222222, 0.72222222, 0.77777778,
        0.77777778, 0.83333333, 0.83333333, 0.88888889, 0.88888889,
        0.94444444, 0.94444444, 1.        , 1.        ]))

f = scipy.interpolate.interp1d(x, y)
print(f(0.90))
# output is 0.9656113509141464, should be 1.0

x2 = np.linspace(0,1,1000)
y2 = f(x2)
plt.plot(x,y)
plt.plot(x2,y2)
"><pre class="notranslate"><code class="notranslate">import numpy as np
import scipy.interpolate

x, y = (np.array([0.00000000e+00, 8.81834215e-05, 1.23456790e-03, 1.23456790e-03,
        2.29276896e-03, 2.29276896e-03, 2.55731922e-03, 2.55731922e-03,
        2.91005291e-03, 2.91005291e-03, 5.02645503e-03, 5.02645503e-03,
        5.11463845e-03, 5.11463845e-03, 2.40740741e-02, 2.40740741e-02,
        2.54850088e-02, 2.54850088e-02, 2.91005291e-02, 2.91005291e-02,
        3.08641975e-02, 3.08641975e-02, 5.41446208e-02, 5.41446208e-02,
        5.91710758e-02, 5.91710758e-02, 7.87477954e-02, 7.87477954e-02,
        9.88536155e-02, 9.88536155e-02, 1.16666667e-01, 1.16666667e-01,
        1.72927690e-01, 1.72927690e-01, 6.92151675e-01, 6.92151675e-01,
        8.38447972e-01, 8.38447972e-01, 1.00000000e+00]),
        np.array([0.        , 0.        , 0.        , 0.05555556, 0.05555556,
        0.11111111, 0.11111111, 0.16666667, 0.16666667, 0.22222222,
        0.22222222, 0.27777778, 0.27777778, 0.33333333, 0.33333333,
        0.38888889, 0.38888889, 0.44444444, 0.44444444, 0.5       ,
        0.5       , 0.55555556, 0.55555556, 0.61111111, 0.61111111,
        0.66666667, 0.66666667, 0.72222222, 0.72222222, 0.77777778,
        0.77777778, 0.83333333, 0.83333333, 0.88888889, 0.88888889,
        0.94444444, 0.94444444, 1.        , 1.        ]))

f = scipy.interpolate.interp1d(x, y)
print(f(0.90))
# output is 0.9656113509141464, should be 1.0

x2 = np.linspace(0,1,1000)
y2 = f(x2)
plt.plot(x,y)
plt.plot(x2,y2)

</code></pre></div>
<p dir="auto">Note that the x value 8.38447972e-01 is present twice in the input; the interp1d function interpolates between the y value corresponding to the <em>first</em> of these x values and the following value, not between the  y value corresponding to the <em>second</em> of these x values and the following value.</p>
<p dir="auto">The same thing happens at x values 7.87477954e-02 and 9.88536155e-02</p>
<p dir="auto">Using mode='previous' and mode='nearest' shows an equivalent bug on this data.</p>
<p dir="auto">Truncating the beginning of the input may make the bug disappear (or cause it to show up for different duplicated x values) depending on how much is truncated.</p>
<h4 dir="auto">Error message:</h4>

<h4 dir="auto">Scipy/Numpy/Python version information:</h4>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="1.4.1 1.18.1 sys.version_info(major=3, minor=6, micro=10, releaselevel='final', serial=0)"><pre class="notranslate"><code class="notranslate">1.4.1 1.18.1 sys.version_info(major=3, minor=6, micro=10, releaselevel='final', serial=0)
</code></pre></div>
<p dir="auto">I would like to reuse the my numpy code that applies a function to a numpy matrix for application to sparse matrices.  However, the or operation (<strong>or</strong>) is not supported for Boolean matrices.  For example, the following code fails after converting the dok matrix to a csr matrix.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import scipy.sparse as sp
x = sp.dok_matrix((10,10))
x[1,2] = 4
y = (x &gt; 3) | (x &lt; 5)"><pre class="notranslate"><code class="notranslate">import scipy.sparse as sp
x = sp.dok_matrix((10,10))
x[1,2] = 4
y = (x &gt; 3) | (x &lt; 5)
</code></pre></div>
<p dir="auto">Would there be interest in adding Boolean logic support for matrices with a bool type?</p>