If you are looking for a quick and (somewhat more) flexible way, try:
lines.view('|S1').reshape(-1, lines.dtype.itemsize)[:, :3].reshape(-1).view('|S3')
What can be used for more arbitrary slicing and slicing.
Time Information:
import numpy as np lines = np.array(['RL5\\Stark_223', 'RL5\\Stark_223', 'RL5\\Stark_223', 'RL5\\Stark_238', 'RL5\\Stark_238', 'RL5\\Stark_238'], dtype='|S27').repeat(100000) %timeit lines.view(np.chararray).ljust(3) 1 loop, best of 3: 231 ms per loop %timeit np.vectorize(lambda x: x[:3])(lines) 1 loop, best of 3: 226 ms per loop %timeit map(lambda s: s[0:3], lines) 1 loop, best of 3: 171 ms per loop %timeit lines.astype('|S3') 100 loops, best of 3: 3.58 ms per loop %timeit lines.view('|S1').reshape(-1, lines.dtype.itemsize)[:, :3].reshape(-1).view('|S3') 100 loops, best of 3: 5.16 ms per loop
source share