Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'package,': 0.03; 'column': 0.07; 'string': 0.09; 'objects,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '52,': 0.16; 'from:addr:pobox.com': 0.16; 'from:addr:skip': 0.16; 'hazard': 0.16; 'iterator': 0.16; 'stuff,': 0.16; 'throw': 0.16; 'timestamps': 0.16; 'weapon': 0.16; 'sender:addr:gmail.com': 0.17; 'bit': 0.19; 'solution.': 0.20; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'entries': 0.24; 'skip': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'sort': 0.25; '15,': 0.26; 'first,': 0.26; 'gets': 0.27; 'header :In-Reply-To:1': 0.27; '(like': 0.30; 'especially': 0.30; 'message-id:@mail.gmail.com': 0.30; 'bunch': 0.31; 'lists': 0.32; 'quite': 0.32; 'beginning': 0.33; 'guess': 0.33; 'subject:time': 0.33; 'table': 0.34; 'skip:d 20': 0.34; 'problem': 0.35; 'something': 0.35; 'received:google.com': 0.35; 'dates': 0.36; 'leads': 0.36; 'so,': 0.37; 'that,': 0.38; 'though,': 0.39; 'sure': 0.39; 'how': 0.40; 'mentioned': 0.61; 'first': 0.61; 'to:addr:gmail.com': 0.65; 'brain': 0.68; 'date,': 0.68; 'records': 0.73; 'ending': 0.78; 'dust': 0.84; 'easier,': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=jZD0mQk3V4zUPR+MpLh6ScPIBDaz27ij7mh0HVBbJmk=; b=KqqbXsCMJXrupync2Mpi4TlSrWEdFCWz76YB6xxBYzTSfjZB2TwPW75DKlW511Xy2A RJRChh4vtqVuSqsoV34p19+JKLBsaHZyKv4IJsN1wpgnaZoVBVwBRt3gYk96wnpC9idy 72VwBQ/6llMohLMPIADYutx6oUuyd974ojJe2ZBqaA/xys5EJW+5YM3/n78paNX4LgZH O0yr2mvcHSFBGF46axi21+VT5z1/KgP7NMUhNOnw1+yc+hi5n6jei8xaBe8zm02tRvY+ bH82MNPLWvshgTz+5wAZUKczO52RHlWVfpHZ94vA6J2sCJG0BeXpfs4EnI0SBesJSW9b lyiQ== MIME-Version: 1.0 X-Received: by 10.50.16.99 with SMTP id f3mr5148456igd.13.1373053436587; Fri, 05 Jul 2013 12:43:56 -0700 (PDT) Sender: skip.montanaro@gmail.com In-Reply-To: <2aa041fe-8226-4fb9-9ce6-b1eb48a19e4d@googlegroups.com> References: <2aa041fe-8226-4fb9-9ce6-b1eb48a19e4d@googlegroups.com> Date: Fri, 5 Jul 2013 14:43:56 -0500 X-Google-Sender-Auth: oU7Eq-1R3uhB2grx3Cy_d-XrAYc Subject: Re: analyzing time From: Skip Montanaro To: noydb Content-Type: text/plain; charset=UTF-8 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373053444 news.xs4all.nl 15909 [2001:888:2000:d::a6]:46850 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50012 > 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