The fastest way to convert a long list of date

Discussion in 'Python' started by loredana.pier@gmail.com, Feb 8, 2009.

  1. Guest

    If I want to convert a single date format I can do:

    import datetime, dateutil.parser
    d = dateutil.parser.parse('2008-09-26)
    print d.strftime('%A %d, %b %y' )

    but if I want convert a long list of time how can do it in the fastest
    way?
    for example:
    from ['2008-09-26’, '2008-09-28’, '2008-09-29’,.............]
    to [’26 sep’,’28 sep’,’29 sep’,................]

    Thank you

    Loredana
     
    , Feb 8, 2009
    #1
    1. Advertising

  2. Chris Rebert Guest

    Re: The fastest way to convert a long list of date

    On Sun, Feb 8, 2009 at 4:46 AM, <> wrote:
    > If I want to convert a single date format I can do:
    >
    > import datetime, dateutil.parser
    > d = dateutil.parser.parse('2008-09-26)
    > print d.strftime('%A %d, %b %y' )
    >
    > but if I want convert a long list of time how can do it in the fastest
    > way?
    > for example:
    > from ['2008-09-26', '2008-09-28', '2008-09-29',.............]
    > to ['26 sep','28 sep','29 sep',................]


    import datetime
    from dateutil.parser import parse
    orig_dates = ['2008-09-26', '2008-09-28', ...]#etc
    converted_dates = [parse(datestr).strftime('%A %d, %b %y' ) for
    datestr in orig_dates]

    Or if an iterator would be okay instead of a list, you can substitute
    the list comprehension for itertools.imap():

    #setup same as before
    from itertools import imap
    converted_dates = imap(lambda datestr: parse(datestr).strftime('%A %d,
    %b %y' ), orig_dates)

    Cheers,
    Chris

    --
    Follow the path of the Iguana...
    http://rebertia.com
     
    Chris Rebert, Feb 8, 2009
    #2
    1. Advertising

  3. Peter Otten Guest

    wrote:

    > If I want to convert a single date format I can do:
    >
    > import datetime, dateutil.parser
    > d = dateutil.parser.parse('2008-09-26)
    > print d.strftime('%A %d, %b %y' )
    >
    > but if I want convert a long list of time how can do it in the fastest
    > way?
    > for example:
    > from ['2008-09-26’, '2008-09-28’, '2008-09-29’,.............]
    > to [’26 sep’,’28 sep’,’29 sep’,................]


    For large source lists the following should be quite fast...

    months = "jan feb mar apr may jun jul aug sep oct dec".split()
    lookup = dict(("%02d-%02d" % (mi+1, d), "%02d-%s" % (d, m)) for mi, m in
    enumerate(months) for d in range(1, 32))

    source = ["2008-09-26", "2008-09-28", "2008-09-29"]
    print [lookup[d[-5:]] for d in source]


    .... but it is also very brittle. Various compromises are possible, but to
    find the best would require that you tell us a bit more about your use
    case. In my experience people are a bit too fast to demand "fast".

    Peter
     
    Peter Otten, Feb 8, 2009
    #3
  4. Guest

    Re: The fastest way to convert a long list of date

    First of all: Thanks for your reply
    My use case is the following:
    I perform a query to DB and the result of query is this;

    >>> print rows

    {‘Label’: {'2009:01:30': 9, '2009:01:28': 13, '2009:01:29': 19,
    '2009:01:26': 1, '2009:01:27': 3, '2009:01:20': 86, '2009:01:22': 3,
    '2009:01:23': 12, '2009:02:07': 3, '2009:02:06': 12, '2009:02:05': 15,
    '2009:02:04': 12, '2009:02:02': 2}}

    I want to make a chart with this data using JSON format where:
    • the key ‘Label’ is the title of chart,
    • rows['Label'].keys() is x values,
    • rows[‘Label’].values() is the y values

    So my goal is:

    1) to fill the bin of chart with no data (for example in this tupla..
    [.., '2009:01:28: 9, '2009:01:30: 13,..].. the date '2009:01:29’ is
    missing and I want to append the value ['2009:01:29’,0] to rows
    [‘Label’])

    2) convert the date format (for example in '%A %d, %b %y')

    3) transform rows dictionary in JSON code

    The point 3) is easy to implement but I’m wondering which is the best
    way to implement the point 1) and 2) ...considering it will be used
    for a web application (CherryPy)?

    thanks,

    Loredana
     
    , Feb 8, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,361
    Michael Borgwardt
    May 30, 2004
  2. Godzilla
    Replies:
    15
    Views:
    548
    jigloo
    Jul 15, 2007
  3. Replies:
    4
    Views:
    700
    Peter Otten
    Oct 30, 2008
  4. Magicloud Magiclouds

    What is the fastest way to convert a object?

    Magicloud Magiclouds, Jul 17, 2007, in forum: Ruby
    Replies:
    13
    Views:
    176
    Magicloud Magiclouds
    Jul 17, 2007
  5. Jarmo Pertman
    Replies:
    0
    Views:
    115
    Jarmo Pertman
    Jan 7, 2009
Loading...

Share This Page