我有一个df
date
2021-03-12
2021-03-17
...
2022-05-21
2022-08-17
我试图添加一列year_week
,但我的一年工作周从2021-06-28
开始,这是七月的第一天
我试过:
df['date'] = pd.to_datetime(df['date'])
df['year_week'] = (df['date'] - timedelta(days=datetime(2021, 6, 24).timetuple()
.tm_yday)).dt.isocalendar().week
我对timedelta
{2021-06-28
的值为1
但后来我遇到了以前&;超过我的开始日期+1年的日期:
2021-03-12 has a value of 38
2022-08-17 has a value of 8
所以看起来有效期是从2021-06-28
+1 year
date year_week
2021-03-12 38 # LY38
2021-03-17 39 # LY39
2021-06-28 1 # correct
...
2022-05-21 47 # correct
2022-08-17 8 # NY8
有没有办法绕过这个问题?由于我按年度每周汇总数据,因此由于过去&;即将到来的日期。我希望在2021-06-28
或LY38
之前的几天有负日期,表示这是去年的年周,相应地,52+的年周或NY8
表示这是下一年的8th
周
我想 熊猫的活动范围可能会有所帮助
这里有一个方法,我增加了两个日期超过一年。您需要从日期列和特定日期的
dayofyear
之间的差值中获取isocalendar
。然后,您可以根据特定日期的year
选择不同的场景。对不同的结果格式使用np.select
相关问题 更多 >
编程相关推荐