filifunk
September 8th, 2009, 04:28 AM
I'm trying to plot some financial data in matplotlib.
I'm plotting simply plot(x, y)
where x are dates
and y are closing prices.
It's not working I get this:
ValueError: Invalid literal for float(): 2009-09-04
So I figured maybe my dates had to be parsed, so I did this:
for i in x:
strptime(i, '%Y-%m-%d')
that didn't work either.
The problem is in my dates. It looks like a formatting issue, but I've only started on python for the last two weeks.
Maybe I need to parse it using the datetime module:
datetime.strptime(date_string, format)¶ (http://docs.python.org/library/datetime.html#datetime.datetime.strptime)Return a datetime (http://docs.python.org/library/datetime.html#datetime.datetime) corresponding to date_string, parsed according to format. This is equivalent to datetime(*(time.strptime(date_string, format)[0:6])). ValueError (http://docs.python.org/library/exceptions.html#exceptions.ValueError) is raised if the date_string and format can’t be parsed by time.strptime() (http://docs.python.org/library/time.html#time.strptime) or if it returns a value which isn’t a time tuple.
- from the python website
but I just imported the module and added 'datetime' before strptime and that didn't change anything.
hmmm.....suggestions?
I'm plotting simply plot(x, y)
where x are dates
and y are closing prices.
It's not working I get this:
ValueError: Invalid literal for float(): 2009-09-04
So I figured maybe my dates had to be parsed, so I did this:
for i in x:
strptime(i, '%Y-%m-%d')
that didn't work either.
The problem is in my dates. It looks like a formatting issue, but I've only started on python for the last two weeks.
Maybe I need to parse it using the datetime module:
datetime.strptime(date_string, format)¶ (http://docs.python.org/library/datetime.html#datetime.datetime.strptime)Return a datetime (http://docs.python.org/library/datetime.html#datetime.datetime) corresponding to date_string, parsed according to format. This is equivalent to datetime(*(time.strptime(date_string, format)[0:6])). ValueError (http://docs.python.org/library/exceptions.html#exceptions.ValueError) is raised if the date_string and format can’t be parsed by time.strptime() (http://docs.python.org/library/time.html#time.strptime) or if it returns a value which isn’t a time tuple.
- from the python website
but I just imported the module and added 'datetime' before strptime and that didn't change anything.
hmmm.....suggestions?