I have dataframe with Date column in the format 1/2/2014 16:00:00. I convert it to 01-02-2014 using this code: df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%m-%d-%Y')
I get the week# in a separate column by
df['Week'] = pd.DatetimeIndex(df['Date']).week but, I get warning saying that .week is deprecated and should use isocalendar().weekinstead.
Question:
I tried to operate isocalendar().week over the Date column but get error. Can someone point about how to use isocalendar() ?
CodePudding user response:
It works well for me...I think you need to keep almost the same format you started with (pd.DatetimeIndex)
pd.DatetimeIndex(df['Date']).isocalendar().week
CodePudding user response:
You should keep on using pd.to_datetime
df['Week'] = pd.to_datetime(df['Date']).dt.isocalendar().week
If you format the Date column to '%d-%m-%Y' this kind of day first representation, you can specify dayfirst argument
df['Week'] = pd.to_datetime(df['Date'], dayfirst=True).dt.isocalendar().week
