2016-01-20 9 views
7

Bei der Verwendung von np.delete wird ein indexError ausgelöst, wenn ein Out-of-Bounds-Index verwendet wird. Wenn ein Out-of-Bounds-Index in einem np.array verwendet wird und das Array als Argument in np.delete verwendet wird, warum löst dies dann keinen indexError aus?Warum Python numpy.delete nicht indexError auslösen, wenn der Out-of-Bounds-Index im np-Array ist

np.delete(np.array([0, 2, 4, 5, 6, 7, 8, 9]), 9) 

dies gibt einen Index-Fehler, wie es sein sollte (Index 9 außerhalb der Grenzen ist)

während

np.delete(np.arange(0,5), np.array([9])) 

und

np.delete(np.arange(0,5), (9,)) 

give:

array([0, 1, 2, 3, 4]) 

Antwort

6

Dies ist ein bekanntes "Feature" und wird in späteren Versionen veraltet sein.

From the source of numpy:

# Test if there are out of bound indices, this is deprecated 
inside_bounds = (obj < N) & (obj >= -N) 
if not inside_bounds.all(): 
    # 2013-09-24, 1.9 
    warnings.warn(
     "in the future out of bounds indices will raise an error " 
     "instead of being ignored by `numpy.delete`.", 
     DeprecationWarning) 
    obj = obj[inside_bounds] 

DeprecationWarning tatsächlich in Python Aktivieren zeigt diese Warnung. Ref

In [1]: import warnings 

In [2]: warnings.simplefilter('always', DeprecationWarning) 

In [3]: warnings.warn('test', DeprecationWarning) 
C:\Users\u31492\AppData\Local\Continuum\Anaconda\Scripts\ipython-script.py:1: De 
precationWarning: test 
    if __name__ == '__main__': 

In [4]: import numpy as np 

In [5]: np.delete(np.arange(0,5), np.array([9])) 
C:\Users\u31492\AppData\Local\Continuum\Anaconda\lib\site-packages\numpy\lib\fun 
ction_base.py:3869: DeprecationWarning: in the future out of bounds indices will 
raise an error instead of being ignored by `numpy.delete`. 
    DeprecationWarning) 
Out[5]: array([0, 1, 2, 3, 4]) 
Verwandte Themen