Seabody
Seabody

Reputation: 1217

numPy - Select column from structured dtype

I am using np.genfromtxt to read in data of mixed type from an external file:

data = np.genfromtxt(filename, dtype='U10,U8,f,f,f,f,f,f')

If the type of my data was homogeneous, I could then access column 2 of my data by doing column_two = data[:,2]. However, since I am using a structured dtype, I can't simply do data[:,2], as this results in an IndexError: too many indices for array. Is there any way I can efficiently select the nth column of data, to use in further processing?

Upvotes: 1

Views: 62

Answers (1)

Seabody
Seabody

Reputation: 1217

So the issue is structured datatypes do not support column indexing. The way I found to rectify it, and the way that feels most organic, is to use "Field Access", viz.

data = np.genfromtxt(filename, dtype=[('date', str), ('time', str), ('az', np.float64), ...)

This way, to access the "time" column, we may simply use data['time'].

Upvotes: 1

Related Questions