Please please help!!
Having a lot of trouble with this code. The task here is to plot some information (High, Low and Medium) against dates associated with them.
The data is found in a .csv file, with the Headings as : Date, High, Medium, Low. The dates are in dd/mm/yyyy format.
So far, I've used genfromtxt to specify the columns and the datatype etc.. However, I think there's a problem with how Python is reading the columns - I keep getting either "Too many indices":
Traceback (most recent call last):
File "F:\Python\A1.py", line 14, in <module>
x = data[:,0]
IndexError: too many indices
OR if I use x = data[;,'Date] I get this:
Traceback (most recent call last):
File "F:\Python\A1.py", line 14, in <module>
x = data[:,'Date']
ValueError: invalid literal for long() with base 10: 'Date'
Here is the complete code:
import pylab as py
import numpy as np
import datetime as dt
import csv
data = np.genfromtxt('F:\\Python\\All.csv', usecols=(0,1,2,3), names=True, skip_header=0, dtype=[('Date', 'S10')]),('High','f8'),('Medium','f8'),('Low','f8')], delimiter = ',')
print data
x = data[:,Date]
y1 = data[:,1]
y2 = data[:,2]
y3 = data[:,3]
Date2 = []
for x in data:
date_format = dt.datetime.strptime((str(x)), '%d/%m/%Y')
Date2.append.date_format
Thanks!
data = np.genfromtxt(...) generates 1-D array of tuple which has three elements. For example, data will look like this.
array([(b'02/03/2015', 3.0, 2.0, 1.0), (b'03/04/2015', 4.0, 3.0, 1.0),
(b'04/05/2015', 10.0, 9.0, 7.0), (b'05/06/2015', 12.0, 4.0, 3.0),
(b'06/07/2015', 2.0, 1.0, 0.0)],
dtype=[('Date', 'S10'), ('High', '<f8'), ('Medium', '<f8'), ('Low', '<f8')])
What you were trying to do was treating data as 2-d array but it is actually 1-d array. So you can do something like this
x = [foo[0] for foo in data]
y1 = [foo[1] for foo in data]
y2 = [foo[2] for foo in data]
y3 = [foo[3] for foo in data]