date list to dates converter

I need a function which will take an N by 3 or N by 6

    > array, where the columns are year, month, day in the
    > first case and those three plus hours, minutes, and
    > seconds in the second case and convert them to matplotlib
    > dates. This would work the same as the MATLAB datenum()
    > function.

Hi Stephen,

How about

    from datetime import datetime
    from pylab import date2num
    x = ...your array
    dts = date2num([ datetime(y,m,d) for y,m,d in x ])

or for the Nx6 case

    dts = date2num([ datetime(y,m,d,h,min,s) for y,m,d,h,min,s in x ])

or more succinctly

    dts = date2num([ datetime(*date) for date in x ])

FYI, date2num is found in matplotlib.dates. I'll take a look at the
matlab datenum docstring in matlab and see about including this
functionality in the matplotlib.dates version.

Cheers,
JDH

John Hunter wrote:

"Stephen" == Stephen Walton <stephen.walton@...267...> writes:
           
   > I need a function which will take an N by 3 or N by 6
   > array... and convert them to matplotlib
   > dates.

   from datetime import datetime
   from pylab import date2num
   x = ...your array
   dts = date2num([ datetime(y,m,d) for y,m,d in x ])

This works fine, John, as do your other ideas as well.

FYI, date2num is found in matplotlib.dates. I'll take a look at the
matlab datenum docstring in matlab and see about including this
functionality in the matplotlib.dates version.

No need. The functionality is already there and I need to begin "thinking in Python." Using tuple unpacking on rows of an array did not occur to me, I admit.

Steve