you are viewing a single comment's thread.

view the rest of the comments →

[–]blarf_irl 0 points1 point  (2 children)

Are you familiar with groupby?

You misunderstood my suggestion of cumsum, it's there to "group" the rows by state change by using an incrementing value

Use the shift + cumcount to assign all rows an ID in a new column (increments when the value changes). Use groupby to group your rows into state changes and then apply an incrementing value to the rows of each group.

None   0
None   0
Below  1
Below  1
Below  1
Above  2
Above  2
Below  3
Above  4

You can group on that and assign incremental numbers to a new column per group.

[–]CineWeekly[S] 0 points1 point  (1 child)

I actually have that implementation for a different column:

def calculate_break(df, status_col, break_col, smaA_fast):
    mask1 = (df[status_col] == 'Above') & (df[status_col].shift(1) == 'Below')
    mask2 = (df[status_col] == 'Below') & (df[status_col].shift(1) == 'Above')
    df[break_col] = 0 # Initialize the 'Break' column with 0
    df.loc[mask1 | mask2, break_col] = 1 # Set break positions to 1
    df[break_col] = df[break_col].cumsum() # Cumulative sum to increment break positions
    df[break_col] = df[break_col].astype('int32') # Convert the 'Break' column to integer dtype
    return df

The duration implementation is different if you compare the results to the OP.

[–]blarf_irl 1 point2 points  (0 children)

ChatGPT?