Say you have dates in the format of %Y%U
, such as 202101
(year 2021, week 1) and you'd like to to use pd.to_datetime
to convert that to a conventional date, such as 2021-01-04
I'm trying to see if there's a better way to handle years w/53 weeks. Consider the following:
from datetime import date
import pandas as pd
df = pd.DataFrame({'week':['202053','202101']})
Here is what I want
print(date.fromisocalendar(2020,53,1), date.fromisocalendar(2021,1,1))
2020-12-28 2021-01-04
This is what pandas will do, which I believe is due to pandas wanting week 53 to be considered week 0 of the next year
print(pd.to_datetime(df.week.astype(str)+'1', format='%Y%W%w').tolist())
[Timestamp('2021-01-04 00:00:00'), Timestamp('2021-01-04 00:00:00')]
So I can do
df['week'] = np.where(df['week'].str.endswith('53'), (df['week'].astype(int)+47).astype(str),df['week'])
print(pd.to_datetime(df.week.astype(str)+'1', format='%Y%W%w').tolist())
[Timestamp('2020-12-28 00:00:00'), Timestamp('2021-01-04 00:00:00')]
So I can add 47 weeks to any date that ends in 53, and I get what I'm expecting. This feels like a very roundabout way to achieve this.
Is there are more standard way to handle this?