Why is np.genfromtxt returning nan when reading the data files?

812 views Asked by At

I have a list containing the strings of my data files. When I read the data I expect to see a numpy array with 3 columns of floats but instead I just get nan.

FILE_NAMES = ['file1.csv', 'file2.csv']
data = np.genfromtxt(FILE_NAMES, delimiter=',', skip_header=1)
print(data)

Here is an example of the data:

More of the same below, float values for all 3 columns

The values in all three of the columns are floats.

All it returns is:

nan

Can someone tell me what I need to change?

1

There are 1 answers

0
hpaulj On

That it didn't give an error initially puzzled me, but then I realized that:

genfromtxt accepts either a filename, or anything that gives it strings, like an open file, or a list of strings. I often use such a list with copy-n-paste file samples.

It's in effect parsing this file

file1.csv
file2.csv

With 1 header line and 1 non-float data line. Hence the nan data.

In [82]: alist = ['csv1.txt','csv2.txt','csv3.txt']
In [85]: data = np.genfromtxt(alist, dtype=None, encoding=True)
In [86]: data
Out[86]: array(['csv1.txt', 'csv2.txt', 'csv3.txt'], dtype='<U8')