Dataframe column: to find (cumulative) local maxima

In the below dataframe the column “CumRetperTrade” is a column which consists of a few vertical vectors (=sequences of numbers) separated by zeros. (= these vectors correspond to non-zero elements of column “Portfolio”). I would like to find the cumulative local maxima of every non-zero vector contained in column “CumRetperTrade”.
To be precise, I would like to transform (using vectorization – or other – methods) column “CumRetperTrade” to the column “PeakCumRet” (desired result) which gives for every vector ( = subset corresponding to ’Portfolio =1 ’) contained in column “CumRetperTrade” the cumulative maximum value of (all its previous) values. The numeric example is below. Thanks in advance!
PS In other words, I guess that we need to use cummax() but to apply it only to the consequent (where ‘Portfolio’ = 1) subsets of ‘CumRetperTrade’

import numpy as np
import pandas as pd
df1 = pd.DataFrame({"Portfolio": [1, 1, 1, 1, 0 , 0, 0, 1, 1, 1],
"CumRetperTrade": [2, 3, 2, 1, 0 , 0, 0, 4, 2, 1],
"PeakCumRet": [2, 3, 3, 3, 0 , 0, 0, 4, 4, 4]})
df1

    Portfolio   CumRetperTrade  PeakCumRet
0   1           2               2
1   1           3               3
2   1           2               3
3   1           1               3
4   0           0               0
5   0           0               0
6   0           0               0
7   1           4               4
8   1           2               4
9   1           1               4

PPS I already asked a similar question previously (Dataframe column: to find local maxima) and received a correct answer to my question, however in my question I did not explicitly mention the requirement of cumulative local maxima

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You only need a small modification to the previous answer:

df1["PeakCumRet"] = (
    df1.groupby(df1["Portfolio"].diff().ne(0).cumsum())
    ["CumRetperTrade"].expanding().max()
    .droplevel(0)
)

expanding().max() is what produces the local maxima.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x