okl
okl

Reputation: 317

pandas convert date to week number

I got this error when trying to convert dateto week Could it be the date values not datetime like values?

data['DATECREATED'].dt.week

error screenshot

Upvotes: 2

Views: 6813

Answers (2)

Joe Rivera
Joe Rivera

Reputation: 363

Series.dt.weekofyear and Series.dt.week have been deprecated. Please call Series.dt.isocalendar() and access the week column instead.

Per the above messaging week of year can no longer be extracted with the above methods. Instead the below should work.

data['DATECREATED'] = pd.to_datetime(data['DATECREATED'])
data['created_week'] = data['DATECREATED'].dt.isocalendar().week

Source: https://pandas.pydata.org/pandas-docs/version/1.5/reference/api/pandas.Series.dt.week.html

Upvotes: 0

jezrael
jezrael

Reputation: 862661

I think you need convert column to datetime first and assign back to column:

data['DATECREATED'] = pd.to_datetime(data['DATECREATED'])
data['new'] = data['DATECREATED'].dt.strftime('%Y%V')
data['CreatedWW'] = data['DATECREATED'].dt.week

Upvotes: 2

Related Questions