weekdays in range

2009-10-19 Thread Jive Dadson

(Sorry if this shows up twice.)

Can someone think of an easy way to calculate the number of weekdays 
between two calendar dates (in Python)?


Thankee.
--
http://mail.python.org/mailman/listinfo/python-list


Re: weekdays in range

2009-10-19 Thread Jive Dadson
I'm using weekdays as a proxy for days when the US stock market is open. 
 (I'll miss holidays.)  The application is pricing CALL and PUT 
options.  Speed is not a problem.  The number of days will typically be 
between 1 and 254.

--
http://mail.python.org/mailman/listinfo/python-list


Re: weekdays in range

2009-10-18 Thread Ben Finney
Jive Dadson notonthe...@noisp.com writes:

 Can someone think of an easy way to calculate the number of weekdays
 between two calendar dates (in Python)?

That depends on what you mean by “weekdays”.

 import datetime
 begin_date = datetime.date(2009, 10, 9)
 end_date = datetime.date(2009, 10, 22)
 import calendar
 print calendar.month(2009, 10)
October 2009
Mo Tu We Th Fr Sa Su
  1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
 end_date - begin_date
datetime.timedelta(13)

If you're expecting to exclude Saturday and Sunday (i.e. if you expect
the above result to be 9 days instead of 13), you can use other
functions of the ‘calendar’ module; try starting with:

 friday_weekday = 4
 len([
... date for date in (
... begin_date + datetime.timedelta(days)
... for days in range((end_date - begin_date).days))
... if calendar.weekday(date.year, date.month, date.day) = 
friday_weekday])
9

-- 
 \   “Selfish, adj. Devoid of consideration for the selfishness of |
  `\  others.” —Ambrose Bierce, _The Devil's Dictionary_, 1906 |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: weekdays in range

2009-10-18 Thread Jive Dadson

Ben Finney wrote:

Jive Dadson notonthe...@noisp.com writes:


Can someone think of an easy way to calculate the number of weekdays
between two calendar dates (in Python)?


That depends on what you mean by “weekdays”.

 import datetime
 begin_date = datetime.date(2009, 10, 9)
 end_date = datetime.date(2009, 10, 22)
 import calendar
 print calendar.month(2009, 10)
October 2009
Mo Tu We Th Fr Sa Su
  1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
 end_date - begin_date
datetime.timedelta(13)

If you're expecting to exclude Saturday and Sunday (i.e. if you expect
the above result to be 9 days instead of 13), you can use other
functions of the ‘calendar’ module; try starting with:

 friday_weekday = 4
 len([
... date for date in (
... begin_date + datetime.timedelta(days)
... for days in range((end_date - begin_date).days))
... if calendar.weekday(date.year, date.month, date.day) = 
friday_weekday])
9



Thanks for your help. For a non-expert at Python, that last compound 
statement is pretty inscrutable.  I am trying to scrute it.  Wish me luck.

--
http://mail.python.org/mailman/listinfo/python-list


Re: weekdays in range

2009-10-18 Thread Ben Finney
Jive Dadson notonthe...@noisp.com writes:

 Ben Finney wrote:
   friday_weekday = 4
   len([
  ... date for date in (
  ... begin_date + datetime.timedelta(days)
  ... for days in range((end_date - begin_date).days))
  ... if calendar.weekday(date.year, date.month, date.day) = 
  friday_weekday])
  9
 

 Thanks for your help. For a non-expert at Python, that last compound
 statement is pretty inscrutable.  I am trying to scrute it.  Wish me
 luck.

I'll help you by showing (liberally editing to make me look
reptrospectively clever) how I built it up:

 import datetime
 import calendar
 import pprint

 begin_date = datetime.date(2009, 10, 9)
 end_date = datetime.date(2009, 10, 22)

 end_date - begin_date
datetime.timedelta(13)
 (end_date - begin_date).days
13
 range((end_date - begin_date).days)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

 begin_date + datetime.timedelta(0)
datetime.date(2009, 10, 9)
 begin_date + datetime.timedelta(1)
datetime.date(2009, 10, 10)
 (begin_date + datetime.timedelta(days)
... for days in range((end_date - begin_date).days))
generator object at 0xf78c08f0

 [
... date for date in (
... begin_date + datetime.timedelta(days)
... for days in range((end_date - begin_date).days))
... ]
[datetime.date(2009, 10, 9), datetime.date(2009, 10, 10), 
datetime.date(2009, 10, 11), datetime.date(2009, 10, 12), datetime.date(2009, 
10, 13), datetime.date(2009, 10, 14), datetime.date(2009, 10, 15), 
datetime.date(2009, 10, 16), datetime.date(2009, 10, 17), datetime.date(2009, 
10, 18), datetime.date(2009, 10, 19), datetime.date(2009, 10, 20), 
datetime.date(2009, 10, 21)]
 pprint.pprint([
... date for date in (
... begin_date + datetime.timedelta(days)
... for days in range((end_date - begin_date).days))
... ])
[datetime.date(2009, 10, 9),
 datetime.date(2009, 10, 10),
 datetime.date(2009, 10, 11),
 datetime.date(2009, 10, 12),
 datetime.date(2009, 10, 13),
 datetime.date(2009, 10, 14),
 datetime.date(2009, 10, 15),
 datetime.date(2009, 10, 16),
 datetime.date(2009, 10, 17),
 datetime.date(2009, 10, 18),
 datetime.date(2009, 10, 19),
 datetime.date(2009, 10, 20),
 datetime.date(2009, 10, 21)]
 len([
... date for date in (
... begin_date + datetime.timedelta(days)
... for days in range((end_date - begin_date).days))
... ])
13

 friday_weekday = 4
 calendar.weekday(begin_date.year, begin_date.month, begin_date.day)
4
 calendar.weekday(end_date.year, end_date.month, end_date.day)
3

 [
... date for date in (
... begin_date + datetime.timedelta(days)
... for days in range((end_date - begin_date).days))
... if calendar.weekday(date.year, date.month, date.day) = 
friday_weekday]
[datetime.date(2009, 10, 9), datetime.date(2009, 10, 12), 
datetime.date(2009, 10, 13), datetime.date(2009, 10, 14), datetime.date(2009, 
10, 15), datetime.date(2009, 10, 16), datetime.date(2009, 10, 19), 
datetime.date(2009, 10, 20), datetime.date(2009, 10, 21)]
 pprint.pprint([
... date for date in (
... begin_date + datetime.timedelta(days)
... for days in range((end_date - begin_date).days))
... if calendar.weekday(date.year, date.month, date.day) = 
friday_weekday])
[datetime.date(2009, 10, 9),
 datetime.date(2009, 10, 12),
 datetime.date(2009, 10, 13),
 datetime.date(2009, 10, 14),
 datetime.date(2009, 10, 15),
 datetime.date(2009, 10, 16),
 datetime.date(2009, 10, 19),
 datetime.date(2009, 10, 20),
 datetime.date(2009, 10, 21)]
 len([
... date for date in (
... begin_date + datetime.timedelta(days)
... for days in range((end_date - begin_date).days))
... if calendar.weekday(date.year, date.month, date.day) = 
friday_weekday])
9

-- 
 \ “We are all agreed that your theory is crazy. The question that |
  `\  divides us is whether it is crazy enough to have a chance of |
_o__)being correct.” —Niels Bohr (to Wolfgang Pauli), 1958 |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: weekdays in range

2009-10-18 Thread Jive Dadson

Wow.  It's a danged tutorial.  Thanks again.  Take a break.
--
http://mail.python.org/mailman/listinfo/python-list


Re: weekdays in range

2009-10-18 Thread Dave Angel

Jive Dadson wrote:
div class=moz-text-flowed style=font-family: -moz-fixedWow.  
It's a danged tutorial.  Thanks again.  Take a break.


/div

Ben Finney's method is a very good approach, and an experienced Python 
programmer would consider it straightforward.


But I have to ask whether the range of dates you might be considering 
could be large.  For example, if you want to know how many week-days 
there are between a date in 1904 and 2207, it'd build a list of some 110 
thousand items.  There are other approaches which would be faster, 
consume much less memory, and be much harder to read.



I'm not offering to debug it, but here's such an approach, subject to 
the old plus/minus one bugs.


def epochweekdays (datetimeobj)
 Given an arbitrary Monday long ago, figure out how many 
weekdays have occurred between that day and the argument

 subtract to get total_days
 return int(totaldays/7) * 5   + max(totaldays%7, 5)

Now, your answer is just
   epochweekdays(b) - epochweekdays(a)

DaveA

--
http://mail.python.org/mailman/listinfo/python-list