Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python, how to know the week number in the year of the day, Saturday as the first day of the week

Tags:

python

pandas

I am using python/pandas, and want to know how to get the week number in the year of one day while Saturday as the first day of the week. i did search a lot, but all the way takes either Monday or Sunday as the first day of week... Please help...thanks

Thanks all! really appreciated all your quick answers..but i have to apology that i am not making my question clearly.

I want to know the week number in the year. for example, 2015-08-09 is week 32 while Monday as first day of week, but week 33 while Saturday as first day of week.

Thanks @Cyphase and everyone, I changed a bit the code of Cyphase and it works.

def week_number(start_week_on, date_=None):
    assert 1 <= start_week_on <= 7  #Monday=1, Sunday=7

    if not date_:
        date_ = date.today()

    __, normal_current_week, normal_current_day = date_.isocalendar()
    print date_, normal_current_week, normal_current_day

    if normal_current_day >= start_week_on:
        week = normal_current_week + 1
    else:
        week = normal_current_week

    return week
like image 928
fuji4900 Avatar asked Aug 13 '15 09:08

fuji4900


2 Answers

If I understand correctly the following does what you want:

In [101]:
import datetime as dt
import pandas as pd
​
df = pd.DataFrame({'date':pd.date_range(start=dt.datetime(2015,8,9), end=dt.datetime(2015,9,1))})
df['week'] = df['date'].dt.week.shift(-2).ffill()
df['orig week'] = df['date'].dt.week
df['day of week'] = df['date'].dt.dayofweek
df

Out[101]:
         date  week  orig week  day of week
0  2015-08-09    33         32            6
1  2015-08-10    33         33            0
2  2015-08-11    33         33            1
3  2015-08-12    33         33            2
4  2015-08-13    33         33            3
5  2015-08-14    33         33            4
6  2015-08-15    34         33            5
7  2015-08-16    34         33            6
8  2015-08-17    34         34            0
9  2015-08-18    34         34            1
10 2015-08-19    34         34            2
11 2015-08-20    34         34            3
12 2015-08-21    34         34            4
13 2015-08-22    35         34            5
14 2015-08-23    35         34            6
15 2015-08-24    35         35            0
16 2015-08-25    35         35            1
17 2015-08-26    35         35            2
18 2015-08-27    35         35            3
19 2015-08-28    35         35            4
20 2015-08-29    36         35            5
21 2015-08-30    36         35            6
22 2015-08-31    36         36            0
23 2015-09-01    36         36            1

The above uses dt.week and shifts by 2 rows and then forward fills the NaN values.

like image 71
EdChum Avatar answered Oct 05 '22 17:10

EdChum


import datetime
datetime.date(2015, 8, 9).isocalendar()[1]
like image 24
KiDo Avatar answered Oct 05 '22 18:10

KiDo