0

I'm trying to plot dates from a csv. file column against three other columns of numbers. I'm new to python and have so far managed to import the columns into python and have tried to read them has an array but i can't seem to append them with the datetime module and plot the dates along the x axis along with my data.

Please can anyone help?

At the minute I keep getting the error message:

Traceback (most recent call last):
File "H:\AppliedGIS\Python\woops.py", line 24, in <module>
     date = datetime.datetime.strptime['x', '%d/%m/%Y']
AttributeError: type object 'datetime.datetime' has no attribute 'datetime'

But i'm sure i'm going wrong in more than one place...

The data itself is formatted in four columns and when printed looks like this: ('04/03/2013', 7.0, 12.0, 17.0) ('11/03/2013', 23.0, 15.0, 23.0).

Here is the complete code

import csv
import numpy as np
import pylab as pl
import datetime
from datetime import datetime
data = np.genfromtxt('H:/AppliedGIS/Python/AssignmentData/GrowthDistribution/full.csv', names=True, usecols=(0, 1, 2, 3), delimiter= ',', dtype =[('Date', 'S10'),('HIGH', '<f8'), ('Medium', '<f8'), ('Low', '<f8')])
print data

x =  [foo['Date'] for foo in data]
y = [foo['HIGH'] for foo in data]
y2 = [foo['Medium'] for foo in data]
y3 = [foo['Low'] for foo in data] 
print x, y, y2, y3

dates = []
for x in data:
    date = datetime.datetime.strptime['x', '%d/%m/%Y']
    dates.append(date)

pl.plot(data[:, x], data[:, y], '-r', label= 'High Stocking Rate')
pl.plot(data[:, x], data[:, y2], '-g', label= 'Medium Stocking Rate')
pl.plot(data[:, x], data[:, y3], '-b', label= 'Low Stocking Rate')
pl.title('Amount of Livestock Grazing per hectare', fontsize=18)
pl.ylabel('Livestock per ha')
pl.xlabel('Date')
pl.grid(True)
pl.ylim(0,100)

pl.show()
Jacktt
  • 13
  • 3

1 Answers1

2

The problem is in the way you have imported datetime.

The datetime module contains a class, also called datetime. At the moment, you are just importing the class as datetime, from which you can use the strptime method, like so:

from datetime import datetime
...
x =  [foo['Date'] for foo in data]
...
dates=[]
for i in x:
    date = datetime.strptime(i,'%d/%m/%Y')
    dates.append(date)

Alternatively, you can import the complete datetime module, and then access the datetime class using datetime.datetime:

import datetime
...
x =  [foo['Date'] for foo in data]
...
dates=[]
for i in x:
    date = datetime.datetime.strptime(i,'%d/%m/%Y')
    dates.append(date)
tmdavison
  • 64,360
  • 12
  • 187
  • 165
  • Brilliant thanks @tom i seem to have run into another problem with format of the time data with this message: 'ValueError: time data 'x' does not match format '%d/%m/%Y''.... any ideas?? – Jacktt Apr 05 '16 at 13:38
  • you are reusing the variable `x`. You first define it as the dates from `data`, but then reuse it in the loop (`for x in data`). try using a different variable name in the loop (see my edited answer above, I used `i`). You also don't need the quotes around `i` in the call to `strptime`. – tmdavison Apr 05 '16 at 13:54