[Numpy-discussion] Trim a numpy array in numpy.

Warren Weckesser warren.weckesser@enthought....
Tue Aug 16 18:44:10 CDT 2011


On Tue, Aug 16, 2011 at 4:51 PM, Hongchun Jin <hongchunjin@gmail.com> wrote:

> *Thanks Derek for  the quick reply. But **I am sorry, I did not make it
> clear in my last email.  Assume I have an array like *
> *
>
> ['CAL_LID_L2_05kmCLay-Prov-V3-01.2008-01-01T00-37-48ZD.hdf'
>
>  'CAL_LID_L2_05kmCLay-Prov-V3-01.2008-01-01T00-37-48ZD.hdf'
>
>  'CAL_LID_L2_05kmCLay-Prov-V3-01.2008-01-01T00-37-48ZD.hdf' ...,
>
>  'CAL_LID_L2_05kmCLay-Prov-V3-01.2008-01-31T23-56-35ZD.hdf'
>
>  'CAL_LID_L2_05kmCLay-Prov-V3-01.2008-01-31T23-56-35ZD.hdf'
>
>  'CAL_LID_L2_05kmCLay-Prov-V3-01.2008-01-31T23-56-35ZD.hdf']
>
> I need to get the sub-string for date and time, for example,
> **
>
> '2008-01-31T23-56-35ZD' in the middle of each element. In more general
> cases, the sub-string could be any part of the string in such an array.  I
> hope to assign the start and stop of the sub-string when I am subsetting it.
>
> *
>

Here's one way:
-----
import numpy as np


def strslice(x, start=None, stop=None, step=None):
    """
    Given a contiguous 1-d numpy array `x` of strings, return a new numpy
    array `y` of strings so that y[k] = x[k][start:stop:step].  `y` contains
a
    copy of the data, not a view.
    """
    slc = slice(start, stop, step)
    x2d = x.view(np.byte).reshape(-1, x.itemsize)
    y2d = x2d[:, slc].copy()
    y = y2d.view('S' + str(y2d.shape[-1])).ravel()
    return y


if __name__ == "__main__":

    x =
np.array(['CAL_LID_L2_05kmCLay-Prov-V3-01.2008-01-01T00-37-48ZD.hdf',

'CAL_LID_L2_05kmCLay-Prov-V3-01.2008-01-01T00-37-48ZD.hdf',

'CAL_LID_L2_05kmCLay-Prov-V3-01.2008-01-01T00-37-48ZD.hdf',

'CAL_LID_L2_05kmCLay-Prov-V3-01.2008-01-31T23-56-35ZD.hdf',

'CAL_LID_L2_05kmCLay-Prov-V3-01.2008-01-31T23-56-35ZD.hdf',

'CAL_LID_L2_05kmCLay-Prov-V3-01.2008-01-31T23-56-35ZD.hdf'])

    print "x:\n", x

    y = strslice(x, start=31, stop=52)
    print "y:\n", y
-----

Output:
-----
x:
['CAL_LID_L2_05kmCLay-Prov-V3-01.2008-01-01T00-37-48ZD.hdf'
 'CAL_LID_L2_05kmCLay-Prov-V3-01.2008-01-01T00-37-48ZD.hdf'
 'CAL_LID_L2_05kmCLay-Prov-V3-01.2008-01-01T00-37-48ZD.hdf'
 'CAL_LID_L2_05kmCLay-Prov-V3-01.2008-01-31T23-56-35ZD.hdf'
 'CAL_LID_L2_05kmCLay-Prov-V3-01.2008-01-31T23-56-35ZD.hdf'
 'CAL_LID_L2_05kmCLay-Prov-V3-01.2008-01-31T23-56-35ZD.hdf']
y:
['2008-01-01T00-37-48ZD' '2008-01-01T00-37-48ZD' '2008-01-01T00-37-48ZD'
 '2008-01-31T23-56-35ZD' '2008-01-31T23-56-35ZD' '2008-01-31T23-56-35ZD']
-----


Warren

*
>
>
> *
> *Best,
>
> Hongchun
> *
>
>
> On Tue, Aug 16, 2011 at 4:39 PM, Derek Homeier <
> derek@astro.physik.uni-goettingen.de> wrote:
>
>> x.astype('|S3')
>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20110816/bb09f8cf/attachment-0001.html 


More information about the NumPy-Discussion mailing list