我有以下DataFrame,其中index是日期,Rate表示当天的给定汇率。我想计算利润,最小值表示此比率是本地最小值(如您所见,日期不完全符合常规逻辑),而max是本地最大值。因此,我需要在最小值时购买,在最大值时出售。现在我想知道利润,即Row.max-Row(-1).min
Rate min max profit
04.10.2016 1.1161 1.1161 NaN NaN
05.10.2016 1.1211 NaN 1.1211 0.0050
07.10.2016 1.1140 1.1140 NaN NaN
10.10.2016 1.1160 NaN 1.1160 0.0020
12.10.2016 1.1020 1.1020 NaN NaN
13.10.2016 1.1038 NaN 1.1038 0.0018
19.10.2016 1.0979 1.0979 NaN NaN
20.10.2016 1.0980 NaN 1.0980 0.0001
21.10.2016 1.0886 1.0886 NaN NaN
24.10.2016 1.0891 NaN 1.0891 0.0005
25.10.2016 1.0872 1.0872 NaN NaN
27.10.2016 1.0927 NaN 1.0927 NaN
28.10.2016 1.0922 1.0922 NaN NaN
我当前的代码如下:
df = pandas.DataFrame.from_dict(exchange_rates, orient='index', dtype='float64', columns=['Rate'])
df['min'] = df.Rate[(df.Rate.shift(1) > df.Rate) & (df.Rate.shift(-1) > df.Rate)]
df['max'] = df.Rate[(df.Rate.shift(1) < df.Rate) & (df.Rate.shift(-1) < df.Rate)]
# df.replace(['NaN'], np.nan, inplace=True)
filtered_df = df[df['min'].notnull() | df['max'].notnull()]
filtered_df['profit'] = df['max'] - df['min'].shift(1)
print(filtered_df)
但是我的方法并没有带来最后的利润。