-1

I have pandas dataframe in the format 10/2018, 10 represents the week of the year and 2018 the year. How do I convert them to complete date which will be march 5th 2018

week/year Date 10/2018 05-03-2018

I have pandas dataframe in the format 10/2018, 10 represents the week of the year and 2018 the year. How do I convert them to complete date which will be march 5th 2018

Please help me for the same. i have tried multiple option but nothing seems to working

week/year Date

10/2018 05-03-2018

tried this:df_2018_to_2022['TimeDesc'] = pd.to_datetime((df_2018_to_2022['week']-1).astype(str) + "6", format="%Y%U%w")

error:

OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1020-04-29 00:00:00
anagha s
  • 323
  • 1
  • 4
  • 15

1 Answers1

0

Here is a proposition with pandas.to_datetime :

out = (
        df
          .join(df["week/year"].str.split("/", expand=True))
          .assign(date= lambda x: pd.to_datetime(x.pop(1).astype(str) + x.pop(0).astype(str) + "1",
                                                 format='%G%V%w').dt.strftime("%d-%m-%Y"))
      )
​

# Output :

print(out)

  week/year        date
0   10/2018  05-03-2018

DataFrame used :

print(df)

  week/year
0   10/2018
Timeless
  • 22,580
  • 4
  • 12
  • 30