Python 5 week buckets

时间:2018-12-03 13:00:15

标签: python pandas datetime timedelta

I have a list of dates for 2-3 calender years in a Dataframe. I want to tag them in 5weekly fashion like below:

    date         5week
    2015-01-01     1
    2015-01-02     1
     .             .
    2015-01-25     2
    2015-01-26     2
     .             .
    2015-02-22     3

or make buckets of 5 weeks intervals. What would be the most elegant way to do this? I am trying a loop with some bugs. the code is:

    for i in range(len(df)):
        if df.loc[i,'week'] < 5:
           df.loc[i,'5we']=0


        elif df.loc[i,'week']%5==0:
           df.loc[i,'5we']=count
              if (df.loc[i,'week']!=df.loc[i-1,'week']):
                    count+=1

        else:
           df.loc[i,'5we']=count

but I think this is a clumsy way to do this even if I get it to work (it is not currently). Please share your expert knowledge.

1 个答案:

答案 0 :(得分:2)

I figured it out. It is rather simple. First I need to extract the week of the year from the date. This can be done by:

    df['week']=df['date'].dt.strftime("%U")

    date       week
    2015-01-01  0
    2015-01-05  1

Now, I need to simply divide the week of the year by 5 and typecast the result into int instead of float:

    df['5week']=df['week'].astype(int)/5
    df['5week']=df['5week'].astype(int)