The fastest way to convert a long list of date

L

loredana.pier

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
 
C

Chris Rebert

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
 
P

Peter Otten

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
 
L

loredana.pier

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;
{‘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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top