numpy delete not deleting element
<p>Under what circumstances would bumpy.delete simply return a copy of the original array, unaltered?</p>

<p>If I do a simple example as follows, then things work as advertised:</p>

<pre><code>&gt;&gt;&gt; a = numpy.arange(0, 10)
&gt;&gt;&gt; numpy.delete(a, [1])
&gt;&gt;&gt; array([0, 2, 3, 4, 5, 6, 7, 8, 9])
</code></pre>

<p>but when I work with an array created from a list by numpy.array() then things don't work anymore.</p>

<pre><code>    scale = [Params.BADVALUE, Params.BADVALUE+1]
    for h in xrange(500, 3001, 500):
        scale.append(h)
    scale += [4000, 5000]
    for h in xrange(7000, 17001, 2000):
        scale.append(h)
    scale.append(upper_bound)
    self.SCALE = numpy.array(scale)
    self.SCALE_WRITE = numpy.delete(self.SCALE, [1000.0])
</code></pre>

<p>self.SCALE_WRITE ends up being identical to self.SCALE</p>

<p>I've checked that self.SCALE is indeed 1-dimensional and that no extra dimensions have been added by mistake.  </p>

<p>It's easy enough to code my way around this but I would still like to know what I'm doing wrong.</p>

<p>Catherine</p>

<p>first, <code>delete</code> does not operate in place:</p>

<pre><code>In [849]: a=np.arange(10)
In [850]: np.delete(a,[1])
Out[850]: array([0, 2, 3, 4, 5, 6, 7, 8, 9])  # returned array
In [851]: a
Out[851]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])  # not change in a
</code></pre>

<p>If I do an out of bounds delete with a scalar, I get an error:</p>

<pre><code>In [853]: a1=np.delete(a,11)
...
IndexError: index 11 is out of bounds for axis 0 with size 10
</code></pre>

<p>But if the delete is a list, it appears the bounds check does not operate (is there a parameter for that?)</p>

<pre><code>In [854]: a1=np.delete(a,[11])
In [855]: a1
Out[855]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
</code></pre>

<p><code>a1</code> is a copy though; changing one of its values does not affect <code>a</code>.</p>

<pre><code>In [858]: a1[-1]=100
In [859]: a1
Out[859]: array([  0,   1,   2,   3,   4,   5,   6,   7,   8, 100])
In [860]: a
Out[860]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
</code></pre>

<p><code>np.delete</code> is a complicated Python function, written to be quite general.  It can be studied.  It is not a fundamental function.  It is not doing anything you can't do just as well with basic array operations like masking, indexing and/or selective copying. And it isn't going to be faster.</p>

<p>================</p>

<p>You can study <code>np.delete</code>.  My memory is that it often does the following:</p>

<p><code>delete</code> via mask for scalar:</p>

<pre><code>In [863]: mask=np.ones(a.shape,dtype=bool)
In [864]: mask[1]=False
In [865]: a[mask]
Out[865]: array([0, 2, 3, 4, 5, 6, 7, 8, 9])
</code></pre>

<p>for list:</p>

<pre><code>In [866]: mask=np.ones(a.shape,dtype=bool)
In [867]: mask[[1,3,5]]=False
In [868]: a[mask]
Out[868]: array([0, 2, 4, 6, 7, 8, 9])
</code></pre>

<p>==================</p>

<p>Recreation of your script with added displays:</p>

<pre><code>In [874]: scale=[1,2]
In [875]: for h in range(500,3001,500):scale.append(h)
In [876]: len(scale)
Out[876]: 8
In [877]: scale+=[4000,5000]
In [878]: for h in range(7000,17001,2000):scale.append(h)
In [879]: len(scale)
Out[879]: 16
In [880]: scale.append(1000)
In [881]: Scale=np.array(scale)
In [882]: Scale.shape
Out[882]: (17,)
In [883]: np.delete(Scale,[1000])
Out[883]: 
array([    1,     2,   500,  1000,  1500,  2000,  2500,  3000,  4000,
        5000,  7000,  9000, 11000, 13000, 15000, 17000,  1000])
</code></pre>

<p>So there are only 17 items in the array, not a 1000.  And as I illustrated with a delete list, it does not raise an error if it is out of bounds.  Hence the <code>delete</code> result is a copy of the input.</p>

<p>By the way I create that same array with an array concatenate</p>

<pre><code>In [886]: np.r_[1, 2, 500:3001:500, [4000,5000], 7000:17001:2000, 1000]
Out[886]: 
array([    1,     2,   500,  1000,  1500,  2000,  2500,  3000,  4000,
        5000,  7000,  9000, 11000, 13000, 15000, 17000,  1000])
</code></pre>

<p>Even sticking with the list I can avoid the loops with <code>extend</code>:</p>

<pre><code>In [887]: scale=[1,2]
In [888]: scale.extend(range(500,3001,500))
In [889]: scale.extend([4000,5000])
In [890]: scale.extend(range(7000,17001,2000))
In [891]: scale.append(1000)
</code></pre>

python arrays numpy