转换“年"和“一年中的一周"要“更新"的列在 pandas

编程入门 行业动态 更新时间:2024-10-23 21:39:25
本文介绍了转换“年"和“一年中的一周"要“更新"的列在 pandas 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

为了将年份和年份的两列转换为日期,我希望这样做:

In order to convert two columns with year and week of year into date I would expect to do something like:

df['formatted_date'] = df.year*100+df.weekofyear df['date'] = pd.to_datetime(df['formatted_date'], format='%Y%w')

但是,它不起作用,给出 ValueError :

However, it does not work, giving ValueError:

ValueError: unconverted data remains: 01

解决方法

我发现解决方法是将一年中的某周转换为一年中的某天,并使用year-dayofyear %Y%j 格式:

df['formatted_date'] = df.year*1000+df.weekofyear*7-6 df['date'] = pd.to_datetime(df['formatted_date'], format='%Y%j')

第一行变得丑陋,但这可以正常工作.一年中的星期在(00,53)范围内.有什么想法,为什么优雅的方式行不通?

The first line becomes ugly, but this works fine. Week of year is in the range (00,53). Any ideas, why is the elegant way not working?

推荐答案

您需要在一周中的某天组合%w -一周内使用%W 的解释:

You need combine %w for day of week - explanation with %W for week:

strftime/:

一年中的一周号(星期一为一周的第一天),以十进制数表示.在新的一年中,第一个星期一之前的所有天均视为在第0周.

Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0.

对于%w :

工作日(十进制),其中0是星期日,6是星期六.

Weekday as a decimal number, where 0 is Sunday and 6 is Saturday.

df = pd.DataFrame({'year':[2015, 2018], 'weekofyear':[10,12]}) dates = df.year*100+df.weekofyear @adde df['date'] = pd.to_datetime(dates.astype(str) + '0', format='%Y%W%w') print (df) year weekofyear formatted_date date 0 2015 10 201510 2015-03-15 1 2018 12 201812 2018-03-25

另一种解决方案:

#added 0 only for demontration, you can remove it df['formatted_date'] = df.year * 1000 + df.weekofyear * 10 + 0 df['date'] = pd.to_datetime(df['formatted_date'], format='%Y%W%w') print (df) year weekofyear formatted_date date 0 2015 10 2015100 2015-03-15 1 2018 12 2018120 2018-03-25

更多推荐

转换“年"和“一年中的一周"要“更新"的列在 pandas

本文发布于:2023-10-28 23:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538085.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:年中   quot   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!