Skip to content
Advertisement

How to get the week number starting from monday of given month in python?

I was trying to calculate the week number starting from first Monday of October. Is there any functions in pandas or datetime to do the calculation efficiently?

MWE

JavaScript

Advertisement

Answer

Shift dates by the number of days to new year, they use standard Monday week number formatting (%W):

JavaScript
  • The offset in days can be calculated manually (I assume the fiscal year start is fixed)
  • The replace part is needed because leftovers from the previous year are considered week 0. The previous week might be 52 or 53, so replacing with NA and then fill forward
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement