I have an existing program using numpy.genfromtxt() to read CSV files. Now I need to add columns to the resulting data type.

I have figured out that genfromtxt() returns a 1-dimensional numpy.ndarray that contains data structures of type numpy.ma.core.mvoid. I can access the data here very well, but I can't append to or resize this mvoid.

from io import StringIO

csvfilecontents = StringIO('''Time,Temp[°C],QC_Temp,Sal[PSU],QC_Sal,Chla[]
2017-06-01T00:00:00+00:00,7.8953,1,6.5534,1
2017-06-01T00:00:15+00:00,7.8943,1,6.5533,1
2017-06-01T00:00:30+00:00,7.8934,1,6.5532,1''')

colnames=['Time', 'Temp', 'QC_Temp', 'Sal', 'QC_Sal']
dtypes=['datetime64[s]', 'f8', 'i8', 'f8', 'i8']
d=np.genfromtxt(csvfilecontents, delimiter=',', skip_header=1, usemask=True, names=colnames, dtype=dtypes)

print ("d is of type {}".format(type(d)))
print ("d[0] is of type {}".format(type(d[0])))

d[0].append(0) # AttributeError: 'mvoid' object has no attribute 'append'
d[0][5] = 0    # Does not work

How should I work with mvoids? Should I use .tolist() and then row by row rebuild the data into another ndarray? Is there an easier way?

0 Answers