为了将年份和年份的两列转换为日期,我希望这样做:
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
发布评论