[英]Pandas: get the monday date from columns containing year and week number
I have a dataframe like:我有一个 dataframe 像:
data = {'year': [2020, 2020, 2021, 2021], 'week': [52, 53, 1, 2]}
df = pd.DataFrame(data=data)
year week
0 2020 52
1 2020 53
2 2021 1
3 2021 2
and I would like to get for each line the Monday (date) of that period so something like:我想为每一行获取该期间的星期一(日期),例如:
year week period
0 2020 52 2020-12-21
1 2020 53 2020-12-28
2 2021 1 2021-01-04
3 2021 2 2021-01-11
What is the correct way to do this in pandas?在 pandas 中执行此操作的正确方法是什么?
The right way to do this is to use ISO date format pattern:正确的做法是使用 ISO 日期格式模式:
df["period"] = pd.to_datetime(
df.year.astype(str) + '-W' + df.week.astype(str) + '-1',
format='%G-W%V-%u')\
.dt.strftime('%Y-%m-%d')
and not并不是
df["period"] = pd.to_datetime(
df.year.astype(str) + '-W' + df.week.astype(str) + '-1',
format='%Y-W%W-%w')\
.dt.strftime('%Y-%m-%d')
Because of ISO week number (cf comments)由于 ISO 周数(cf 评论)
You can try with:您可以尝试:
data = {'year': [2020, 2020, 2021, 2021], 'week': [52, 53, 1, 2]}
df = pd.DataFrame(data=data)
df['date combined']=df['year'].astype(str)+'-'+df['week'].astype(str)+ '-1'
df['date of Monday']=pd.to_datetime(df['date combined'], format='%Y-%W-%w')
print(df)
result:结果:
year week date combined date
0 2020 52 2020-52-1 2020-12-28
1 2020 53 2020-53-1 2021-01-04
2 2021 1 2021-1-1 2021-01-04
3 2021 2 2021-2-1 2021-01-11
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.