python pandas - 检索cummax()处的索引时间戳值

时间:2016-11-26 14:15:04

标签: python pandas

我正在检索以下数据帧的cummax()值,

read -p "Good Morning, Please enter your file type name for sorting [ENTER]:" extension
if cd /Users/christopherdorman/desktop; then
    destination="folder$extension"
    # ensure the destination folder exists
    mkdir -p "$destination"
    if mv  -v unsorted/*."$extension" "$destination"; then
        echo "Good News, Your files have been successfully processed"
    fi
fi

使用以下公式

                     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

有没有办法检索每行的cummax()行的时间戳?类似于.idxmax()但是对于cummax()?

1 个答案:

答案 0 :(得分:2)

这可能就是你要找的东西。

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