date2num , ValueError: ordinal must be >= 1
Question: 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.locator() 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 :)


Answer: 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()
    

![enter image description here](http://i.stack.imgur.com/1e5Ad.png)

