numpy.genfromtxt produces array of what looks like tuples, not a 2D array—why?

前端 未结 1 622
太阳男子
太阳男子 2020-11-29 08:22

I\'m running genfromtxt like below:

date_conv = lambda x: str(x).replace(\":\", \"/\")
time_conv = lambda x: str(x)

a = np.genfromtxt(input.txt         


        
相关标签:
1条回答
  • 2020-11-29 08:30

    What is returned is called a structured ndarray, see e.g. here: http://docs.scipy.org/doc/numpy/user/basics.rec.html. This is because your data is not homogeneous, i.e. not all elements have the same type: the data contains both strings (the first two columns) and floats. Numpy arrays have to be homogeneous (see here for an explanation).

    The structured array 'solves' this constraint of homogeneity by using tuples for each record or row, that's the reason the returned array is 1D: one series of tuples, but each tuple (row) consists of several fields, so you can regard it as rows and columns. The different columns are accessible as a['nameofcolumn'] e.g. a['Julian_Day'].

    The reason that it returns a 2D array when removing the converters for the first two columns is that in that case, genfromtxt regards all data of the same type, and a normal ndarray is returned (the default type is float, but you can specify this with the dtype argument).

    EDIT: If you want to make use of the column names, you can use the names argument (and set the skip_header at only three):

    a2 = np.genfromtxt("input.txt", delimiter=',', skip_header=3, names = True, dtype = None,
                      usecols=[0, 1] + radii_indices, converters={0: date_conv, 1: time_conv})
    

    the you can do e.g.:

    >>> a2['Dateddmmyyyy']
    array(['06/03/2006', '06/03/2006', '18/03/2006', '19/03/2006',
           '19/03/2006', '19/03/2006', '19/03/2006', '19/03/2006',
           '19/03/2006', '19/03/2006'], 
          dtype='|S10')
    
    0 讨论(0)
提交回复
热议问题