Victoria Smith
Victoria Smith

Reputation: 1

NumPy - Unpacking data with different dtypes from a file using genfromtxt

I am trying to read in a file where the 1st column is a date string and columns 2-4 are regular floating point numbers. If I do

data = np.genfromtxt("infile.csv", delimiter=','),

I get an array of 4x rows in file where all the values in column 1 are (quite rightly) NaNs. So to get my date as a string I tried

data = np.genfromtxt("infile.csv", delimiter=',', dtype=("|S20",float,float,float))

Outcome is a 1D array with all four columns of each row now as one element of the array.

Can anyone explain what I'm doing wrong please?

Upvotes: 0

Views: 443

Answers (1)

pseudocubic
pseudocubic

Reputation: 1039

There are better ways to do this, but since we don't know how you plan to handle the date string a quick (to write, not to run depending on the amount of data) and dirty way would be to use some loops:

data_tup = tuple(np.empty(len(data), dtype=data.dtype[i]) for i in range(len(data[0])))
for i, line in enumerate(data):
    for j, item in enumerate(line):
        data_tup[j][i] = item

This will give you a tuple of ndarrays with data from each column.

Upvotes: 1

Related Questions