I use numpy.genfromtxt to load data from a csv file, and this file has mixed data type, like the following:
date,value1,value2
1997-02, 432, 1
1997-03, 300, 1
1997-04, 432, 0
I use the following command to load data:
data = numpy.genfromtxt('data/test.csv', dtype=None, delimiter=',', skip_header=1)
and I get
array([(b'1997-02', 432, 1), (b'1997-03', 300, 1), (b'1997-04', 432, 0)],
dtype=[('f0', 'S7'), ('f1', '<i8'), ('f2', '<i8')])
Now I want only the date of this data, how can I slice the numpy array with mixed type? I tried to use data[:,0] but it does not work
You can use the field name:
data['f0']
# array([b'1997-02', b'1997-03', b'1997-04'],
# dtype='|S7')
And further for your reading purpose, I think you want to specify the names = True instead of skip_header so that the first line will be read in as the field names of the structured array:
data = np.genfromtxt('data/test.csv', dtype=None, delimiter=',', names = True)
data
# array([(b'1997-02', 432, 1), (b'1997-03', 300, 1), (b'1997-04', 432, 0)],
# dtype=[('date', 'S7'), ('value1', '<i8'), ('value2', '<i8')])
Now you can access date as:
data['date']
# array([b'1997-02', b'1997-03', b'1997-04'],
# dtype='|S7')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With