简体   繁体   English

Pandas:从包含年和周数的列中获取星期一日期

[英]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.

相关问题 从当月的周数获取周开始日期(周一或周日) - Get week start date(Monday or Sunday) from week number of the month 从 Python (pandas) 的日期列中获取周开始日期(星期一)? - Get week start date (Monday) from a date column in Python (pandas)? 在熊猫中将“年”和“年中的星期”列转换为“日期” - Converting “year” and “week of year” columns to “date” in Pandas Python - 从星期几、年份和周数中获取日期 - Python - Get date from day of week, year, and week number 考虑到每周的开始日期是python中的星期一,如何获取一年中每周的开始日期和星期数? - How to get week start dates and week number of each week in a year considering start day of the week is Monday in python? Pandas 从日期中提取年中的星期和年份 - Pandas extract week of year and year from date Python:从熊猫的周数和年份计算一周的开始日期和结束日期 - Python: Calculating Start Date and End Date of a week from the Week Number & Year From Pandas Python: Pandas dataframe 获取周数所属的年份而不是日期的年份 - Python: Pandas dataframe get the year to which the week number belongs and not the year of the date 从 pandas 中的年/周/工作日获取日期 - Getting date from year/week/weekday in pandas 在Pandas中将包含年和周数的字符串转换为日期时间 - Converting a string containing year and week number to datetime in Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM