I am retrieving the cummax() value of the following dataframe,
exit_price trend netgain high low MFE_pr
exit_time
2000-02-01 01:00:00 1400.25 -1 1.00 1401.50 1400.25 1400.25
2000-02-01 01:30:00 1400.75 -1 0.50 1401.00 1399.50 1399.50
2000-02-01 02:00:00 1400.00 -1 1.25 1401.00 1399.75 1399.50
2000-02-01 02:30:00 1399.25 -1 2.00 1399.75 1399.25 1399.25
2000-02-01 03:00:00 1399.50 -1 1.75 1400.00 1399.50 1399.25
2000-02-01 03:30:00 1398.25 -1 3.00 1399.25 1398.25 1398.25
2000-02-01 04:00:00 1398.75 -1 2.50 1399.00 1398.25 1398.25
2000-02-01 04:30:00 1400.00 -1 1.25 1400.25 1399.00 1398.25
2000-02-01 05:00:00 1400.25 -1 1.00 1400.50 1399.25 1398.25
2000-02-01 05:30:00 1400.50 -1 0.75 1400.75 1399.50 1398.25
with the following formula
trade ['MFE_pr'] = np.nan
trade ['MFE_pr'] = trade ['MFE_pr'].where(trade ['trend']<0, trade.high.cummax())
trade ['MFE_pr'] = trade ['MFE_pr'].where(trade ['trend']>0, trade.low.cummin())
is there a way to retrieve the timestamp of the row at which cummax() is taken from for each row? something similar to .idxmax() but for cummax() ?
This is probably what you are looking for.
import pandas as pd
import datetime
df = pd.DataFrame({'a': [1, 2, 1, 3, 2, 5, 4, 3, 5]},
index=pd.DatetimeIndex(start=
datetime.datetime.fromtimestamp(0),
periods=9, freq='D'))
df['cummax'] = df.a.cummax()
df['timestamp'] = df.index
df = df.merge(df.groupby('cummax')[['timestamp']].first().reset_index(), on='cummax')
df.rename(columns={'timestamp_y': 'max_timestamp'}, inplace=True)
df.index=df.timestamp_x.values
del df['timestamp_x']
print(df)
a cummax max_timestamp
1970-01-01 03:00:00 1 1 1970-01-01 03:00:00
1970-01-02 03:00:00 2 2 1970-01-02 03:00:00
1970-01-03 03:00:00 1 2 1970-01-02 03:00:00
1970-01-04 03:00:00 3 3 1970-01-04 03:00:00
1970-01-05 03:00:00 2 3 1970-01-04 03:00:00
1970-01-06 03:00:00 5 5 1970-01-06 03:00:00
1970-01-07 03:00:00 4 5 1970-01-06 03:00:00
1970-01-08 03:00:00 3 5 1970-01-06 03:00:00
1970-01-09 03:00:00 5 5 1970-01-06 03:00:00
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.