[Tutor] Need help with dates in Python

Prasad, Ramit ramit.prasad at jpmchase.com
Wed Mar 9 23:42:58 CET 2011


My major problem is your use of datetime. Why are you comparing each day/month/year manually?

import datetime
>>d = datetime.datetime(2001,01,15)
>>c = datetime.datetime(2001,01,14)
>>d > c
True

that makes your entire program look like:
max = datetime.date(1900, 1, 1)
min = datetime.date(2500, 12, 31)
for line in open ('test2.txt','r'):
       data = line.rstrip().split()
       a = data[9]
       b = data[4]
       (year, month, day) = b.split('-')
        date = datetime.date(year,month,day)
        if date > max:
              max = date
       if date < min:
              min = date
       print 'blah' + str(min) + str(max) + 'blah'

Not sure  exactly how you are printing, but hopefully you get the point.


Ramit



Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

From: tutor-bounces+ramit.prasad=jpmchase.com at python.org [mailto:tutor-bounces+ramit.prasad=jpmchase.com at python.org] On Behalf Of James Reynolds
Sent: Wednesday, March 09, 2011 2:57 PM
To: nookasree ponamala
Cc: tutor at python.org
Subject: Re: [Tutor] Need help with dates in Python


On Wed, Mar 9, 2011 at 1:34 PM, nookasree ponamala <nookasree at yahoo.com<mailto:nookasree at yahoo.com>> wrote:
Hi,
I'm new to Python programming. I've changed the code to below, but still it is not working, Could you pls. make the corrections in my code.

import datetime
t = ()
tot = []
min = datetime.date(2008, 1, 1)
max = datetime.date(2012, 12, 31)
for line in open ('test2.txt','r'):
       data = line.rstrip().split()
       a = data[9]
       b = data[4]
       (year, month, day) = b.split('-')
       year = int(year)
       month = int(month)
       day = int(day)
       t = (year,month,day)
               if t < max:
               maxyr = max
               if t > min:
               minyr = min
               t = (a,b,maxyr,minyr)
               tot.append(t)
               print t
Thanks
Sree.

--- On Wed, 3/9/11, Andre Engels <andreengels at gmail.com<mailto:andreengels at gmail.com>> wrote:

> From: Andre Engels <andreengels at gmail.com<mailto:andreengels at gmail.com>>
> Subject: Re: [Tutor] Need help with dates in Python
> To: "nookasree ponamala" <nookasree at yahoo.com<mailto:nookasree at yahoo.com>>
> Cc: tutor at python.org<mailto:tutor at python.org>
> Date: Wednesday, March 9, 2011, 2:16 PM
> On Wed, Mar 9, 2011 at 9:21 AM,
> nookasree ponamala <nookasree at yahoo.com<mailto:nookasree at yahoo.com>>
> wrote:
> > Hi,
> >
> > I need help in finding the minimum date and maximum
> date in a file.
> > Here is my test file:
> > s.no<http://s.no>:   dt1     amt     id1     id2
> > 452     2010-02-20      $23.26      059542
>      06107
> > 452     2010-02-05      $20.78      059542
>      06107
> > 451     2010-02-24      $5.99       059542
>      20151
> > 452     2010-02-12      $114.25     839745
>      98101
> > 452     2010-02-06      $28.00      839745
>      06032
> > 451     2010-02-12      $57.00      839745
>      06269
> >
> > I want to get the minimum and maximum dt1 for each
> id1
> >
> > Required result:
> >
> > id1 mindate maxdate
> > 059542  2010-02-24      2010-02-20
> > 839745  2010-02-06      2010-02-12
> >
> > Code: The code I tried. It doesn't work though.
> >
> > import sys
> > import os
> > t = ()
> > tot = []
> > maxyr = 2012
> > minyr = 2008
> > maxday = 31
> > minday = 1
> > maxmon = 12
> > minmon = 1
> >
> > for line in open ('test2.txt','r'):
> >        data = line.rstrip().split()
> >        a = data[3]
> >        b = data[1]
> >        (year, month, day) = b.split('-')
> >        year = int(year)
> >        month = int(month)
> >        day = int(day)
> > if year > maxyr:
> >        maxyr = year
> > elif year < minyr:
> >        minyr = year
> > if month > maxmon:
> >        maxmon = month
> >        elif month < minmon:
> >        minmon = month
> >        if day > maxday:
> >        maxday = day
> >        elif day < minday:
> >        minday = day
> >        max = (maxyr,maxmon,maxday)
> >        min = (minyr,minmon,minday)
> >        t = (a,b,max,min)
> >        tot.append(t)
> >        print t
> >
> > Could you pls. help me with this.
>
> I see several things go wrong. Here a list, which may well
> not be complete:
>
> * You want the mindate and maxdate for each id1, but you
> remember only
> a single minyr, maxyr etcetera. There's no way that that is
> going to
> work.
> * You initialize minyr etcetera to a date before the first
> date you
> will see, nd maxyr etcetera to a date after the last date.
> This means
> that you will never find an earlier respectively later one,
> so they
> would never be changed. You should do it exactly the other
> way around
> - minyr etcetera should be _later_ than any date that may
> occur, maxyr
> etcetera _earlier_.
> * You move "if year > maxyr" back to the left. This
> means that it is
> not part of the loop, but is executed (only) once _after_
> the loop has
> been gone through
> * year < minyear should be "if", not "elif": it is
> possible that the
> new date is both the first _and_ the last date that has
> been found
> (this will be the case with the first date)
> * You change maxyear, maxmonth and maxday independently.
> That is not
> what you are trying to do - you want the last date, not the
> highest
> year, highest month and highest day (if the dates were
> 2001-12-01,
> 2011-11-03 and 2005-05-30, you want the maximum date to be
> 2011-11-03,
> not 2011-12-30). You should thus find a way to compare the
> *complete
> date* and then if it is later than the maxdate or earlier
> than the
> mindate change the *complete date*
> * At the end you show (well, in this case you don't because
> it is
> under "if month > maxmon") a quadruple consisting of
> id1, current
> date, lowest date and highest date - EACH time. You want
> only the
> triple and only after the last date of some value of id1
> has been
> parsed (best to do that after all lines have been parsed)
> * The code as shown will lead to a syntax error anyway
> because you did
> not indent extra after "elif month < minmon:", "if day
> > maxday:" and
> "elif day < minday:".
>
>
>
>
> --
> André Engels, andreengels at gmail.com<mailto:andreengels at gmail.com>
>



_______________________________________________
Tutor maillist  -  Tutor at python.org<mailto:Tutor at python.org>
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor






Other than some of the previous touched upon commentary, and going slightly beyond your question of "why doesn't this work" you can parse text to datetime objects directly in python by using the datetime.datetime.strptime function. In your case, it would look something like this: dt1 = datetime.datetime.strptime(dt1, '%Y-%m-%d'). This part, '%Y-%m-%d', is telling python how you should expect to see the datetimes in text version (4 digit year, 2 digit month, 2 digit day separated by dashes). You can then do comparisons directly the datetime object.

Personally though, taking this to the next level, I would represent each line from the date you care about as a class, if you are familiar with how to use classes that is, with soemthing that looks like this:

class id:

    def __init__(self,sno,dt1,amt,id1,id2):

        self.sno = sno
        self.dt1 = datetime.datetime.strptime(dt1, '%Y-%m-%d')
        self.amt = amt
        self.id1 = id1
        self.id2 = id2

    def print_all(self):
        print self.sno, self.dt1, self.amt, self.id1, self.id2



Even if you aren't familiar with how to use classes, I would definitely consider using strptime instead of all those splits and whatnot.







This communication is for informational purposes only. It is not
intended as an offer or solicitation for the purchase or sale of
any financial instrument or as an official confirmation of any
transaction. All market prices, data and other information are not
warranted as to completeness or accuracy and are subject to change
without notice. Any comments or statements made herein do not
necessarily reflect those of JPMorgan Chase & Co., its subsidiaries
and affiliates.

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law. If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED. Although this transmission and any
attachments are believed to be free of any virus or other defect
that might affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by JPMorgan Chase &
Co., its subsidiaries and affiliates, as applicable, for any loss
or damage arising in any way from its use. If you received this
transmission in error, please immediately contact the sender and
destroy the material in its entirety, whether in electronic or hard
copy format. Thank you.

Please refer to http://www.jpmorgan.com/pages/disclosures for
disclosures relating to European legal entities.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110309/40ae789b/attachment-0001.html>


More information about the Tutor mailing list