我想将日期时间数据聚合到每周级别。以下适用于
dt.week
,但我收到警告
dt.week
已被弃用。我还没有成功使用isocalendar。你推荐什么?
以下是我想按周汇总的数据示例:
import pandas as pd
times = ['2021-01-28 08:00:00', '2021-01-26 08:00:00', '2021-01-25 08:00:00']
quantities = [40, 12, 43]
df = pd.DataFrame({'date':['2021-01-28 08:00:00','2021-01-26 08:00:00','2021-02-25 08:00:00'],
'quantity':[40, 12, 43]}) # 52 in one week, 43 in the other week
df['date'] = pd.to_datetime(df['date'])
df
这个
dt.week
方法有效,但抛出警告:
df['week_num'] = df['date'].dt.week
df.groupby('week_num').sum('quantity')
后果
第4周
有一个
数量52
和
第8周
有一个
数量43
.
警告内容如下
<ipython-input-50-d66026a79596>:1: FutureWarning: Series.dt.weekofyear and Series.dt.week have been deprecated. Please use Series.dt.isocalendar().week instead. df['week_num'] = df['date'].dt.week