Most, if not all the functions in np.char apply existing str methods to each element of the array. It's a little faster than direct iteration (or vectorize) but not drastically so.
There isn't a string slicer; at least not by that sort of name. Closest is indexing with a slice:
In [274]: 'astring'[1:3]
Out[274]: 'st'
In [275]: 'astring'.__getitem__
Out[275]: <method-wrapper '__getitem__' of str object at 0xb3866c20>
In [276]: 'astring'.__getitem__(slice(1,4))
Out[276]: 'str'
An iterative approach can be with frompyfunc (which is also used by vectorize):
In [277]: a = numpy.array(['hello', 'how', 'are', 'you'])
In [278]: np.frompyfunc(lambda x:x[1:3],1,1)(a)
Out[278]: array(['el', 'ow', 're', 'ou'], dtype=object)
In [279]: np.frompyfunc(lambda x:x[1:3],1,1)(a).astype('U2')
Out[279]:
array(['el', 'ow', 're', 'ou'],
dtype='<U2')
I could view it as a single character array, and slice that
In [289]: a.view('U1').reshape(4,-1)[:,1:3]
Out[289]:
array([['e', 'l'],
['o', 'w'],
['r', 'e'],
['o', 'u']],
dtype='<U1')
I still need to figure out how to convert it back to 'U2'.
In [290]: a.view('U1').reshape(4,-1)[:,1:3].copy().view('U2')
Out[290]:
array([['el'],
['ow'],
['re'],
['ou']],
dtype='<U2')
The initial view step shows the databuffer as Py3 characters (these would be bytes in a S or Py2 string case):
In [284]: a.view('U1')
Out[284]:
array(['h', 'e', 'l', 'l', 'o', 'h', 'o', 'w', '', '', 'a', 'r', 'e', '',
'', 'y', 'o', 'u', '', ''],
dtype='<U1')
Picking the 1:3 columns amounts to picking a.view('U1')[[1,2,6,7,11,12,16,17]] and then reshaping and view. Without getting into details, I'm not surprised that it requires a copy.
routines.charseems to be missing it. I edited the question to make this more clear.[a[s:e] for a, s, e in zip(a_array, s_array, e_array)]? I was hoping that thenumpy.stringsoperations would provide something like that. I am trying to remove prefixes from strings, andnumpy.strings.startswith(a, b)is so tantalizing close -- I'd just need to clip those prefixes inbwhen the strings inastart with them...