简体   繁体   中英

how to convert Week/Year (10/2018) to date format

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM