1 回答
TA贡献1895条经验 获得超7个赞
根据您的日期是否在索引中,您可以注释掉df1 = df.reset_index()。另外,如果索引DateTimeIndex在索引上,请检查其格式是否正确。如果格式不正确,则可以使用更改数据类型df.index = pd.to_datetime(df.index)。然后,您应该设置为更改Diff列df1.groupby(pd.Grouper(key='Index', freq='M'))['Diff'].diff(),然后使用完整数据框更改 groupby:
输入:
import pandas as pd
df = pd.DataFrame({'Diff': {'2019-03-14 11:32:21.583000+00:00': 2,
'2019-04-14 11:32:21.600000+00:00': 14,
'2019-05-14 11:32:21.600000+00:00': 27}})
df.index.name = 'Index'
df.index = pd.to_datetime(df.index)
代码:
df1 = df.reset_index()
df1['Diff'] = df1.groupby(pd.Grouper(key='Index', freq='M'))['Diff'].diff()
df1 = df1.groupby(pd.Grouper(key='Index', freq='M'))['Diff'].max().reset_index()
df1
输出:
Index Diff
0 2019-03-31 00:00:00+00:00 2.0
1 2019-04-30 00:00:00+00:00 1.0
2 2019-05-31 00:00:00+00:00 8.0
添加回答
举报