Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #50012

Re: analyzing time

References <2aa041fe-8226-4fb9-9ce6-b1eb48a19e4d@googlegroups.com>
Date 2013-07-05 14:43 -0500
Subject Re: analyzing time
From Skip Montanaro <skip@pobox.com>
Newsgroups comp.lang.python
Message-ID <mailman.4306.1373053444.3114.python-list@python.org> (permalink)

Show all headers | View raw


> I have a table with a column of type date, with dates and time combined (like '1/6/2013 3:52:69PM'), that spans many months.  How would I pull out records that are the first and last entries per day?

You mentioned "table" and "column", which leads me to think you are
dealing with data in a SQL database.  If so, that would likely change
the problem solution significantly.

If you have something like lists of string data in Python though, you
might want to make sure your timestamps are in a normalized form
first, so you can accurately sort by the timestamps.  For that, my
weapon of choice is the dateutil package, and in particular, the
dateutil.parser module:

>>> x = dateutil.parser.parse("1/6/2013 3:52:59PM")
>>> x
datetime.datetime(2013, 1, 6, 15, 52, 59)
>>> print x
2013-01-06 15:52:59

Once your timestamps are represented as Python datetime objects, the
problem gets a bit easier, especially if you want to find the
beginning and ending timestamps of a bunch of dates.  Sort, then throw
some itertools.groupby pixie dust at it. My ancient, reptilian brain
has never quite grokked all that iterator stuff, so I won't hazard a
guess how to spell the exact solution.

Skip

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

analyzing time noydb <jenn.duerr@gmail.com> - 2013-07-05 12:18 -0700
  Re: analyzing time Neil Cerutti <neilc@norwich.edu> - 2013-07-05 19:35 +0000
  Re: analyzing time Skip Montanaro <skip@pobox.com> - 2013-07-05 14:43 -0500
  Re: analyzing time Gary Herron <gherron@digipen.edu> - 2013-07-05 12:47 -0700
  Re: analyzing time Terry Reedy <tjreedy@udel.edu> - 2013-07-05 15:54 -0400

csiph-web