diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-08-25 20:21:29 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-08-25 20:21:29 +0000 |
commit | 7dd9a565c2fc24b49b1bb0193a9e826294c1f876 (patch) | |
tree | f7db4e06872ff87b61cf02245799ef486d2b6b8c /numpy/lib/function_base.py | |
parent | b4e76e3499cb660e6d8e64688bab66c668d0d6c9 (diff) | |
download | numpy-7dd9a565c2fc24b49b1bb0193a9e826294c1f876.tar.gz |
Added a delete function
Diffstat (limited to 'numpy/lib/function_base.py')
-rw-r--r-- | numpy/lib/function_base.py | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 172dddfea..30ad12a45 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -7,13 +7,14 @@ __all__ = ['logspace', 'linspace', 'histogram', 'bincount', 'digitize', 'cov', 'corrcoef', 'msort', 'median', 'sinc', 'hamming', 'hanning', 'bartlett', 'blackman', 'kaiser', 'trapz', 'i0', 'add_newdoc', 'add_docstring', 'meshgrid', + 'delete' ] import types import numpy.core.numeric as _nx from numpy.core.numeric import ones, zeros, arange, concatenate, array, \ asarray, asanyarray, empty, empty_like, asanyarray, ndarray -from numpy.core.numeric import ScalarType, dot, where, newaxis +from numpy.core.numeric import ScalarType, dot, where, newaxis, intp from numpy.core.umath import pi, multiply, add, arctan2, \ frompyfunc, isnan, cos, less_equal, sqrt, sin, mod, exp from numpy.core.fromnumeric import ravel, nonzero, choose, sort @@ -22,6 +23,7 @@ from numpy.lib.shape_base import atleast_1d from numpy.lib.twodim_base import diag from _compiled_base import _insert, add_docstring from _compiled_base import digitize, bincount +from arraysetops import setdiff1d #end Fernando's utilities @@ -1007,4 +1009,43 @@ def meshgrid(x,y): y = y.reshape(numRows,1) Y = y.repeat(numCols, axis=1) return X, Y + +def delete(arr, obj, axis=-1): + """Delete sub-arrays from an axis + + Return a new array with the sub-arrays (i.e. rows or columns) + deleted along the given axis as specified by obj + + obj may be a slice_object (s_[3:5:2]) or an integer + or an array of integers indicated which sub-arrays to + remove. + + Example: + >>> arr = [[3,4,5], + [1,2,3], + [6,7,8]] + + >>> delete(arr, 1) + array([[3,5], + [1,3], + [6,8]) + >>> delete(arr, 1, 0) + array([[3,4,5], + [6,7,8]]) + """ + arr = asarray(arr) + ndim = arr.ndim + slobj = [slice(None)]*ndim + N = arr.shape[axis] + if isinstance(obj, slice): + obj = arange(obj.start or 0, obj.stop or N, + obj.step or 1, dtype=intp) + else: + obj = array(obj, dtype=intp, copy=0, ndmin=1) + + all = arange(N, dtype=intp) + obj = setdiff1d(all, obj) + slobj[axis] = obj + slobj = tuple(slobj) + return arr[slobj] |