I want to create a new variable that tells what week(nth week) of the month a certain date belongs to. For example, December 2020 has 5 weeks and Dec 8, 2020 belongs to the 2nd week of December. I already have date and month columns in my table and want to created a new column called "NthWeek". For example I want something like this as my output.
Date Month NthWeek
2020-12-08 12 2nd
2020-12-15 12 3rd
My current code is as follows and it works well for month like Dec 2020 but returns in accurate amount for a month like Oct 2020.
df['NthWeek'] = df['Date'].apply(lambda d: (d.day-1) // 7 + 1)
For example, October 5, 2020 is actually in the 2nd week of October, but my current calculation will just give me 1 (first week). Is there any way I can use a built-in calendar to get correct Nth weeks for all instances? How do I edit my code to make it work?
I'd appreciate any help on this.