1 回答

TA貢獻1895條經(jīng)驗 獲得超7個贊
根據(jù)您的日期是否在索引中,您可以注釋掉df1 = df.reset_index()。另外,如果索引DateTimeIndex在索引上,請檢查其格式是否正確。如果格式不正確,則可以使用更改數(shù)據(jù)類型df.index = pd.to_datetime(df.index)。然后,您應(yīng)該設(shè)置為更改Diff列df1.groupby(pd.Grouper(key='Index', freq='M'))['Diff'].diff(),然后使用完整數(shù)據(jù)框更改 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
添加回答
舉報