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 it
df['Date'] = df['Date'].apply(pd.datetools.normalize_date)
so the date now looks like - 2015-06-17 in the data frame column
and now I like to convert that to a week number.
Thanks in advance
The isocalendar() function in Pandas returns the ISO year, week number, and weekday from a Timestamp object (an equivalent of Python's datetime object).
print(dt. strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0. print(dt.
Just access the dt
week attribute:
In [286]: df['Date'].dt.week Out[286]: 0 25 dtype: int64 In [287]: df['Week_Number'] = df['Date'].dt.week df Out[287]: Date Week_Number 0 2015-06-17 25
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With