Week of year is not correctly shown

73 Views Asked by At

I have a time frame data from 2005 uptil now. While converting date to week, it is not converting week correctly. Showing week 0 at start of year e.g. 2008/01/01 is indicated by week 0.

I first import excel file into pandas df and parse date column.

df = pd.read_excel('graph.xls',
                 parse_dates=['date'])

Datatype of date column is:

df['date'].dtypes

Output is dtype('<M8[ns]')

Then i convert date to week using:

df['Week'] = df['date'].dt.strftime('%U')
df.head()

This shows week at start of year say 2007/01/01 to be 0 instead of 1.

1

There are 1 best solutions below

0
On
df['Week'] = df['date'].dt.strftime('%U').astype('float64') + 1