[Python] How can I slice each element of a numpy array of strings?

Discussão em 'Python' iniciado por Stack, Junho 20, 2025.

  1. Stack

    Stack Membro Participativo

    Numpy has some very useful string operations, which vectorize the usual Python string operations.

    Compared to these operation and to pandas.str, the numpy strings module seems to be missing a very important one: the ability to slice each string in the array. For example,

    a = numpy.array(['hello', 'how', 'are', 'you'])
    numpy.char.sliceStr(a, slice(1, 3))
    >>> numpy.array(['el', 'ow', 're' 'ou'])


    Am I missing some obvious method in the module with this functionality? Otherwise, is there a fast vectorized way to achieve this?

    Continue reading...

Compartilhe esta Página