Any good idea on how to traverse with Datetime objects?

1,018 views
Skip to first unread message

Bjorn Madsen

unread,
Jul 23, 2012, 10:16:13 AM7/23/12
to cam...@googlegroups.com
Hi Campug,

Does anyone out there have a good pythonic idea on how to traverse through datetime (python 3.2.3)?

>>> startDate # 2000-1-1
datetime.datetime(2000, 1, 1, 0, 0)
>>> endDate # 2000-12-31
datetime.datetime(2000, 12, 31, 0, 0)
>>> timestep # every 2 hours
datetime.timedelta(0, 7200)
>>> 

Unfortunately the following solution doesn't work as intended:

dictionary={}
for i in range(startDate, endDate, timestep):      # <<<< TypeError: 'datetime.datetime' object cannot be interpreted as an integer
    dictionary[i]=someValue




Kind Regards,

--
Bjorn 

Stephen Paulger

unread,
Jul 23, 2012, 10:50:32 AM7/23/12
to cam...@googlegroups.com
I'm not sure you'd call it pythonic but it works at least.

>>> from datetime import datetime, timedelta
>>> start = datetime(2000, 1, 1)
>>> end = datetime(2000, 1, 2)
>>> current = start
>>> while current < end:
...     print current
...     current += timedelta(hours=2)
...
2000-01-01 00:00:00
2000-01-01 02:00:00
2000-01-01 04:00:00
2000-01-01 06:00:00
2000-01-01 08:00:00
2000-01-01 10:00:00
2000-01-01 12:00:00
2000-01-01 14:00:00
2000-01-01 16:00:00
2000-01-01 18:00:00
2000-01-01 20:00:00
2000-01-01 22:00:00


--
You received this message because you are subscribed to the Google Groups "Cambridge and East Anglian Python Users Group" group.
To post to this group, send email to cam...@googlegroups.com.
To unsubscribe from this group, send email to campug+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/campug?hl=en.

Bjorn Madsen

unread,
Jul 23, 2012, 10:58:23 AM7/23/12
to cam...@googlegroups.com
Looks neat enough to me. Thanks Stephen.
Kind Regards, 
Bjorn

David Reynolds

unread,
Jul 23, 2012, 11:02:28 AM7/23/12
to cam...@googlegroups.com
You might want to look at python-dateutil http://labix.org/python-dateutil/


--
David Reynolds
da...@reynoldsfamily.org.uk
> --
> You received this message because you are subscribed to the Google Groups "Cambridge and East Anglian Python Users Group" group.
> To post to this group, send email to cam...@googlegroups.com (mailto:cam...@googlegroups.com).
> To unsubscribe from this group, send email to campug+un...@googlegroups.com (mailto:campug+un...@googlegroups.com).

Stephen Paulger

unread,
Jul 23, 2012, 11:09:31 AM7/23/12
to cam...@googlegroups.com
Maybe it's slightly more python to do it as a generator.

>>> def date_range(start, end, step):
...     while start < end:
...             yield start
...             start += step
...
>>> from datetime import datetime, timedelta
>>> date_range(datetime(2000, 1, 1), datetime(2000, 1, 2), timedelta(hours=2))
<generator object date_range at 0xb734d4dc>

I suppose you could replace the condition with "while end is None or start < end:" that way you could pass None as the second argument to make it generate forever.

Steve

On 23 July 2012 15:50, Stephen Paulger <stephen...@newspeak.org.uk> wrote:

Dougal Matthews

unread,
Jul 23, 2012, 10:49:28 AM7/23/12
to cam...@googlegroups.com
Why not make a datetime aware range? Something like this; http://dpaste.com/774212/

(Just typed that directly into the browser, it probably wont work)

Dougal


On Monday, 23 July 2012 at 15:16, Bjorn Madsen wrote:

> --
> You received this message because you are subscribed to the Google Groups "Cambridge and East Anglian Python Users Group" group.
> To post to this group, send email to cam...@googlegroups.com (mailto:cam...@googlegroups.com).
> To unsubscribe from this group, send email to campug+un...@googlegroups.com (mailto:campug+un...@googlegroups.com).

Bjorn Madsen

unread,
Jul 23, 2012, 4:12:59 PM7/23/12
to cam...@googlegroups.com
Hi Dougal,
That was clever! Splendid idea!
Thanks.
Bjorn

def daterange(start, end, step):
    current = start
    while current < end:
        current += step
        yield current

startdate = datetime(2000, 1, 1, 0, 0)
enddate = datetime(2000, 12, 31, 0, 0)
timestep = timedelta(0, 7200)


for i in daterange(startdate, enddate, timestep):
    pass


To post to this group, send email to cam...@googlegroups.com.
To unsubscribe from this group, send email to campug+un...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/campug?hl=en.




--
Bjorn Madsen
Researcher Complex Systems Research


Didrik Pinte

unread,
Aug 3, 2012, 11:09:30 AM8/3/12
to cam...@googlegroups.com
Depending on what you want to do, you should consider the datetime
support of numpy:

http://docs.scipy.org/doc/numpy/reference/arrays.datetime.html (look for arange)

-- Didrik

On 23 July 2012 22:12, Bjorn Madsen
Didrik Pinte +32 475 665 668
+44 1223 969515
Enthought Europe dpi...@enthought.com
Scientific Computing Solutions http://www.enthought.com
Reply all
Reply to author
Forward
0 new messages