[英]Parse Year Week columns to Date
I have a data frame with columns Year and Week that I am trying to parse to date into a new column called Date.我有一个包含 Year 和 Week 列的数据框,我试图将其解析为一个名为 Date 的新列。
import datetime
df['Date']=datetime.datetime.fromisocalendar(df['Year'], df['Week'], 1)
But this generates the following error: 'cannot convert the series to <class 'int'>'.但这会产生以下错误:“无法将系列转换为 <class 'int'>”。
My desired outcome is to give the Sunday Date of each week.我想要的结果是给出每周的星期日日期。
For example:例如:
Year: 2022年份:2022
Week: 01周:01
Expected Date: 2022-01-02预计日期:2022-01-02
I know there are similar posts to this already, and I have tried to manipulate, but I was unsuccessful.我知道已经有类似的帖子,并且我试图操纵,但我没有成功。
Thanks for the help!谢谢您的帮助!
You can do你可以做
Yw = df['Year'].astype(str) + df['Week'].astype(str) + '0'
df['Date'] = pd.to_datetime(Yw, format='%Y%U%w')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.