converting a pandas date to week number

前端 未结 4 1648
轮回少年
轮回少年 2020-12-02 16:40

I would like to extract a week number from data in a pandas dataframe.

The date format is datetime64[ns]

I have normalized the date to remove the time from i

4条回答
  •  抹茶落季
    2020-12-02 17:20

    Here is another possibility using strftime. strftime.org is a good resource.

    df['Week_Number'] = df['Date'].dt.strftime('%U')
    

    '%U' represents the week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0.

    If you have dates from multiple years, I recommend creating a Year-Week combination

    df['Year-Week'] = df['Date'].dt.strftime('%Y-%U')
    

提交回复
热议问题