简体   繁体   English

如何将周/年(10/2018)转换为日期格式

[英]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.我有 pandas dataframe 格式为 10/2018,10 代表一年中的一周,2018 年代表一年。 How do I convert them to complete date which will be march 5th 2018我如何将它们转换为完成日期,即 2018 年 3 月 5 日

week/year Date 10/2018 05-03-2018周/年 日期 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.我有 pandas dataframe 格式为 10/2018,10 代表一年中的一周,2018 年代表一年。 How do I convert them to complete date which will be march 5th 2018我如何将它们转换为完成日期,即 2018 年 3 月 5 日

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 :这是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: #Output:

print(out)

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

DataFrame used: DataFrame 使用过:

print(df)

  week/year
0   10/2018

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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