【发布时间】:2019-12-24 03:59:53
【问题描述】:
我有一个数据框,其列名 order date 包含从 2014 年 7 月到 2015 年 6 月的日期,格式为 2014-10-17 15:11:54。使用 datetime 我从日期中提取了周数。但是,我将 2014 年 7 月的起始周设为 27,而不是 2015 年 1 月的起始周从第 1 周开始。我想要的是 2014 年 7 月,因为第 1 周持续到 2015 年 6 月并以 53 结束。
df['Week'] = df.order_date.dt.week
使用上面的代码获取之后的周数,以获取 2014 年 7 月使用的 1
def time_period(x):
if df.Week >= 26:
return df.Week -25
else:
return df.Week +28
df['week_serial'] = df.Week.apply(lambda x: time_period(x))
这给出了一个错误 - Series 的真值是模棱两可的。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。
【问题讨论】:
标签: python python-3.x pandas datetime