开发者

date2num , ValueError: ordinal must be >= 1

开发者 https://www.devze.com 2023-03-12 01:36 出处:网络
I\'m using the matplotlib candlestick module which requires the time to be passed as a float day format . I`m using date2num to convert it, before :

I'm using the matplotlib candlestick module which requires the time to be passed as a float day format . I`m using date2num to convert it, before :

This is my code :

import csv
import sys
import math
import numpy as np
import datetime
from optparse import OptionParser
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import matplotlib.mlab as mlab
import matplotlib.dates as mdates
from matplotlib.finance import candlestick
from matplotlib.dates import date2num

datafile = 'historical_data/AUD_Q10_1D_500.csv'
print 'loading', datafile
r = mlab.csv2rec(datafile, delimiter=';')
quotes = [date2num(r['date']),r['open'],r['close'],r['max'],r['min']]


candlestick(ax, quotes, width=0.6)

plt.show()

( here is the csv file : http://db.tt/MIOqFA0 )

This is what the doc says :

candlestick(ax, quotes, width=0.20000000000000001, colorup='k', colordown='r', alpha=1.0) quotes is a list of (time, open, close, high, low, ...) tuples. As long as the first 5 elements of the tuples are these values, the tuple can be as long as you want (eg it may store volume). time must be in float days format - see date2num

Here is the full error log :

Traceback (most recent call last):
File
"/usr/lib/python2.6/site-packages/matplotlib/backends/backend_qt4agg.py",
line 83, in paintEvent
FigureCanvasAgg.draw(self) File
"/usr/lib/python2.6/site-packages/matplotlib/backends/backend_agg.py",
line 394, in draw
self.figure.draw(self.renderer) File
"/usr/lib/python2.6/site-packages/matplotlib/artist.py",
line 55, in draw_wrapper draw(artist,
renderer, *args, **kwargs) File
"/usr/lib/python2.6/site-packages/matplotlib/figure.py",
line 798, in draw func(*args) File
"/usr/lib/python2.6/site-packages/matplotlib/artist.py",
line 55, in draw_wrapper draw(artist,
renderer, *args, **kwargs) File
"/usr/lib/python2.6/site-packages/matplotlib/axes.py", line 1946, in draw a.draw(renderer)
File
"/usr/lib/python2.6/site-packages/matplotlib/artist.py",
line 55, in draw_wrapper draw(artist,
renderer, *args, **kwargs) File
"/usr/lib/python2.6/site-packages/matplotlib/axis.py", line 971, in draw tick_tups = [ t for
t in self.iter_ticks()] File
"/usr/lib/python2.6/site-packages/matplotlib/axis.py", line 904, in iter_ticks majorLocs =
self.major.l开发者_如何学Pythonocator() File
"/usr/lib/python2.6/site-packages/matplotlib/dates.py",
line 743, in __call__ self.refresh()
File
"/usr/lib/python2.6/site-packages/matplotlib/dates.py",
line 752, in refresh dmin, dmax =
self.viewlim_to_dt() File
"/usr/lib/python2.6/site-packages/matplotlib/dates.py",
line 524, in viewlim_to_dt return
num2date(vmin, self.tz),
num2date(vmax, self.tz) File
"/usr/lib/python2.6/site-packages/matplotlib/dates.py",
line 289, in num2date if not
cbook.iterable(x): return
_from_ordinalf(x, tz) File "/usr/lib/python2.6/site-packages/matplotlib/dates.py",
line 203, in _from_ordinalf dt =
datetime.datetime.fromordinal(ix)
ValueError: ordinal must be >= 1

If I run a quick :

for x in r['date']:
    print str(x) + "is :" + str(date2num(x))

it outputs something like :

2010-06-12is :733935.0
2010-07-12is :733965.0
2010-08-12is :733996.0

which sound ok to me :)


Read the docstring a bit more carefully :)

quotes is a list of (time, open, close, high, low, ...) tuples.

What's happening is that it expects each item of quotes to be a sequence of (time, open, close, high, low).

You're passing in 5 long arrays, it expects a long sequence of 5 items.

You just need to zip your input.

import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
from matplotlib.finance import candlestick
from matplotlib.dates import date2num

datafile = 'Downloads/AUD_Q10_1D_500.csv'
r = mlab.csv2rec(datafile, delimiter=';')

quotes = zip(date2num(r['date']),r['open'],r['close'],r['max'],r['min'])

fig, ax = plt.subplots()
candlestick(ax, quotes, width=0.6)

plt.show()

date2num , ValueError: ordinal must be >= 1


Seems like you're passing it a float. And in the error message you provide (full message next time please!) it appears that matplotlib is simply delegating the conversion to datetime.datetime.fromordinal.

I don't have a Python 3 installation to test this with, but when I tried to convert a float to a datetime object using datetime.datetime.fromordinal in 2.6, I got a deprecation warning. Then I tried it on ideone and got this:

Traceback (most recent call last):
  File "prog.py", line 2, in <module>
    print(datetime.datetime.fromordinal(5.5))
TypeError: integer argument expected, got float

So perhaps it's choking on the float.


I think your problem is here:

r = mlab.csv2rec(datafile, delimiter=';')

You need to skip the first line of the csv, which means you need:

r = mlab.csv2rec(datafile, delimiter=';', skiprows=1)

Technically this is incorrect, Ubuntu has an older version of the library, and the OP's version has the two lines below, but it was my original answer

I would make sure you're using the most recent version of matplotlib.

So that I could reproduce this issue, I downloaded and installed the latest version and I noticed that the line number of the offending piece of code had been changed to 179. I also noticed that the value is cast to int immediately before fromordinal is called (this gives a lot of credence to senderle's answer).

(line 178-179 of most recent matplotlib in Ubuntu repository)

ix = int(x)
dt = datetime.datetime.fromordinal(ix)

If upgrading is not an option, then you should cast to an int first.

0

精彩评论

暂无评论...
验证码 换一张
取 消