Overview
I'm working on a dataframe where df["Pivots"] alternates between 1 and -1 whenever a high or low has previously been identified by a zigzag indicator.
I am trying to implement the following with Pandas on a dataframe and modify the relevant rows when df["Pivots"] has previously (incorrectly) been assigned a value of 1 which marks a high but another row actually has a higher High value.
Please see the screenshots below for a visual representation of the data and the desired output.
Pseudocode
If the current row has a -1 in
df["Pivots"]rows_between = index < current row and index > last pivot value in
df["Pivots"]which will be a 1If
df.Highin rows_between >df["Pivot Price"]in the current row then actual_high isdf[High].max()in rows_between.Remove the 1 from
df["Pivots"]& value fromdf["Pivot Price"]in current row and add it todf["Pivots"]&df["Pivot Price"]in row which is actual_high
Example
In this example df.High in rows 2023-10-08 is actual_high and higher than df["Pivot Price"] in row 2023-09-24.
This is the original dataframe.
This is the desired output:
Actual dataframe will contain many rows and this is just a Minimal, Reproducible Example.
Code
df.to_dict()
{'Open': {Timestamp('2023-09-24 00:00:00', freq='W-SUN'): 1.0427,
Timestamp('2023-10-01 00:00:00', freq='W-SUN'): 1.0586,
Timestamp('2023-10-08 00:00:00', freq='W-SUN'): 1.0314,
Timestamp('2023-10-15 00:00:00', freq='W-SUN'): 1.0669,
Timestamp('2023-10-22 00:00:00', freq='W-SUN'): 1.0058,
Timestamp('2023-10-29 00:00:00', freq='W-SUN'): 0.9966},
'High': {Timestamp('2023-09-24 00:00:00', freq='W-SUN'): 1.0621,
Timestamp('2023-10-01 00:00:00', freq='W-SUN'): 1.0609,
Timestamp('2023-10-08 00:00:00', freq='W-SUN'): 1.0714,
Timestamp('2023-10-15 00:00:00', freq='W-SUN'): 1.0679,
Timestamp('2023-10-22 00:00:00', freq='W-SUN'): 1.0198,
Timestamp('2023-10-29 00:00:00', freq='W-SUN'): 0.9966},
'Low': {Timestamp('2023-09-24 00:00:00', freq='W-SUN'): 1.0383,
Timestamp('2023-10-01 00:00:00', freq='W-SUN'): 1.0297,
Timestamp('2023-10-08 00:00:00', freq='W-SUN'): 1.0285,
Timestamp('2023-10-15 00:00:00', freq='W-SUN'): 1.004,
Timestamp('2023-10-22 00:00:00', freq='W-SUN'): 0.9941,
Timestamp('2023-10-29 00:00:00', freq='W-SUN'): 0.938},
'Close': {Timestamp('2023-09-24 00:00:00', freq='W-SUN'): 1.0577,
Timestamp('2023-10-01 00:00:00', freq='W-SUN'): 1.0297,
Timestamp('2023-10-08 00:00:00', freq='W-SUN'): 1.0666,
Timestamp('2023-10-15 00:00:00', freq='W-SUN'): 1.0053,
Timestamp('2023-10-22 00:00:00', freq='W-SUN'): 0.9988,
Timestamp('2023-10-29 00:00:00', freq='W-SUN'): 0.9528},
'Pivots': {Timestamp('2023-09-24 00:00:00', freq='W-SUN'): 1,
Timestamp('2023-10-01 00:00:00', freq='W-SUN'): 0,
Timestamp('2023-10-08 00:00:00', freq='W-SUN'): 0,
Timestamp('2023-10-15 00:00:00', freq='W-SUN'): 0,
Timestamp('2023-10-22 00:00:00', freq='W-SUN'): 0,
Timestamp('2023-10-29 00:00:00', freq='W-SUN'): -1},
'Pivot Price': {Timestamp('2023-09-24 00:00:00', freq='W-SUN'): 1.0621,
Timestamp('2023-10-01 00:00:00', freq='W-SUN'): nan,
Timestamp('2023-10-08 00:00:00', freq='W-SUN'): nan,
Timestamp('2023-10-15 00:00:00', freq='W-SUN'): nan,
Timestamp('2023-10-22 00:00:00', freq='W-SUN'): nan,
Timestamp('2023-10-29 00:00:00', freq='W-SUN'): 0.938},
'Date': {Timestamp('2023-09-24 00:00:00', freq='W-SUN'): Timestamp('2023-09-24 00:00:00'),
Timestamp('2023-10-01 00:00:00', freq='W-SUN'): Timestamp('2023-10-01 00:00:00'),
Timestamp('2023-10-08 00:00:00', freq='W-SUN'): Timestamp('2023-10-08 00:00:00'),
Timestamp('2023-10-15 00:00:00', freq='W-SUN'): Timestamp('2023-10-15 00:00:00'),
Timestamp('2023-10-22 00:00:00', freq='W-SUN'): Timestamp('2023-10-22 00:00:00'),
Timestamp('2023-10-29 00:00:00', freq='W-SUN'): Timestamp('2023-10-29 00:00:00')}}
For reference, this is the code that generates these pivots.

