antonio_zeus antonio_zeus - 4 months ago 58
Python Question

text file mdates.strpdate2num error

I keep getting an error using the numpy loadtxt converter.

Your help is greatly appreciated

import numpy as np
import time
import datetime
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
from matplotlib.finance import candlestick
from matplotlib.dates import strpdate2num
import urllib2


## global variables
eachStock = 'AAPL','GOOG','MSFT','AMZN','CMG'



for stock in eachStock:

stockFile = stock+'.txt'
date, closep, highp, lowp, openp, volume = np.loadtxt(eachStock, delimiter=',', unpack=True,
converters={ 0: mdates.strpdate2num('%Y%m%d')})

dFrame = Series(closep)


here is the first line in my text file

20040322,13.5200,13.6800,12.6100,12.6850,15850720


here is the error I keep getting

Traceback (most recent call last):
File "C:\Users\antoniozeus\Desktop\BuyAndHold.py", line 27, in <module>
converters={ 0: mdates.strpdate2num('%Y%m%d')})
File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 796, in loadtxt
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "C:\Python27\lib\site-packages\matplotlib\dates.py", line 233, in __call__
return date2num(datetime.datetime(*time.strptime(s, self.fmt)[:6]))
File "C:\Python27\lib\_strptime.py", line 454, in _strptime_time
return _strptime(data_string, format)[0]
File "C:\Python27\lib\_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data 'AAPL' does not match format '%Y%m%d'

Answer

It seems like you mistyped stockFile (filename) as eachStock.

date, closep, highp, lowp, openp, volume = np.loadtxt(
    stockFile, delimiter=',', unpack=True,
    converters={ 0: mdates.strpdate2num('%Y%m%d')})