elzzup
elzzup

Reputation: 63

Pandas: Sorting by week number and year string

I had a list of dates that turned into week number and years using;

dfweek['weeknum'] = df['Date'].dt.strftime('%U_%Y')

This would output: 34_2019

34 being the 34th week of 2019

How would I go about sorting data by this string in chronological order since the order comes out:

00_2018
00_2019
01_2018
01_2019

I tried converting back to datetime by:

dfweek['weeknum1'] = pd.to_datetime(dfweek['weeknum'], format = '%W_%Y')

This kept returning the error: ValueError: Cannot use '%W' or '%U' without day and year

Tried adding a day in the form of %w just to see what happens

dfweek['weeknum'] = df['Date'].dt.strftime('%U_%Y_%w')
dfweek['weeknum1'] = pd.to_datetime(dfweek['weeknum'], format = '%W_%Y_%w')

but it just spits back the original date without the week number

My desired output would be

00_2018
01_2018
02_2018
...
51_2019
52_2019

Upvotes: 0

Views: 1273

Answers (1)

SeaBean
SeaBean

Reputation: 23217

You can use the following for the sorting:

dfweek = dfweek.assign(weeknum1= df['Date'].dt.strftime('%Y_%U')).sort_values('weeknum1')

Here, we made a temporary column weeknum1 using format e.g. '2018_00' and then sort using this format. As a result, it is sorting in year + week number as required.

Upvotes: 3

Related Questions