converting a pandas date to week number

前端 未结 4 1657
轮回少年
轮回少年 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:19

    Pandas has its .dayofyear and .weekofyear functionality, which can be applied straight away to the output of pandas.to_datetime(df['column_name']), giving type "Timestamp" as the output.

    import pandas as pd
    df['formatted_date'] = pd.to_datetime(df['datetime'])
    df['day_of_year'] = df.formatted_date.apply(lambda x: x.dayofyear)
    df['week_of_year'] = df.formatted_date.apply(lambda x: x.weekofyear)
    

提交回复
热议问题