Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting a pandas date to week number

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

like image 634
tony Avatar asked Jul 02 '15 09:07

tony


People also ask

Where is the week number in pandas?

The isocalendar() function in Pandas returns the ISO year, week number, and weekday from a Timestamp object (an equivalent of Python's datetime object).

How do I get the week number in Python?

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.


1 Answers

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 
like image 66
EdChum Avatar answered Oct 22 '22 16:10

EdChum