Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python count business weeks

Given a start date, how can I determine how many "business weeks" it's been with Python? I can't just divide by 7 because that won't give me the correct answer.

An example would be a start date of Aug 1 2012 to the current date (Aug 13 2012) would output 3 weeks.

I'm basically trying to figure out from the start of the football season, what the current week (integer) is.

I've tried messing around with Pythons datetime module, but it's been to no avail.

like image 271
imns Avatar asked Aug 14 '12 02:08

imns


People also ask

How do YOu count business days in Python?

Use the busday_count() function of a numpy library The busday_count() function counts the number of business days between the start date and end date, not including the day of the end dates.

How do I count weeks in Python?

dt = datetime. strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday print(dt. strftime("%W")) # '00' Monday as the 1st day of the week.

How many weeks is a Python month?

Hence, it can be said that on average, 1 month = 4 weeks and 2 days, or 1 month = 413 4 1 3 weeks.


2 Answers

Try using datetime.weekday, datetime.isoweekday to get the current day of the week or use the more complete datetime.isocalendar to also get the current week of the year and using those as offsets to calculate an aligned difference.

So you can have a function like this:

def week_difference(start, end):
    assert start <= end
    start_year, start_week, start_dayofweek = start.isocalendar()
    end_year, end_week, end_dayofweek = end.isocalendar()

    return ((end_year - start_year) * 52) - start_week + end_week

With usage like this:

import datetime as dt
# same week
In [1]: week_difference(dt.datetime(2012, 8, 1),  dt.datetime(2012, 8, 1))
Out[1]: 0

# your example (see note below) 
In [2]: week_difference(dt.datetime(2012, 8, 1),  dt.datetime(2012, 8, 13))
Out[2]: 2

# across years
In [3]: week_difference(dt.datetime(2011, 8, 1),  dt.datetime(2012, 8, 13))
Out[3]: 54

# year boundary: second last business week of 2011, to first business week of 2012
# which is the same business week as the last business week of 2011
In [4]: week_difference(dt.datetime(2011, 12, 20),  dt.datetime(2012, 1, 1))
Out[4]: 1

In [5]: week_difference(dt.datetime(2011, 12, 18),  dt.datetime(2012, 1, 1))
Out[5]: 2

You can add 1 to your week output depending on your chosen semantic of what a week difference should be.

like image 180
Preet Kukreti Avatar answered Oct 27 '22 17:10

Preet Kukreti


When you get your datetime object...

import datetime
today = datetime.datetime.now()
start = datetime.datetime(2012, 9, 5)

...you can additionally use isocalendar...

today.isocalendar()
>>> (2012, 33, 1)
start.isocalendar()
>>> (2012, 36, 3)

This 3-tuple returns the year, week of the year, and the day of the week. If you just care about a single year, then you can simply subtract the "week of the year" values (I might be off by one here..). Otherwise, you'll need to get the year involved.

like image 36
Mark Hildreth Avatar answered Oct 27 '22 16:10

Mark Hildreth