I have a time series data frame, and I want to list the days when in three consecutive days, High was the highest the second day and the same thing but the value of Low was the lowest the second day. I tried these two lines but I hardcoded the date after viewing the data. I want to know if there is a way to not hardcode it.
df_highest = df.loc['1986-03-17':'1986-03-19', 'High'].max()
df_lowest= df.loc['1986-03-24':'1986-03-26', 'Low'].min()
Here's a piece of data.
CodePudding user response:
Try using the diff
function combined with greater than (gt
) or less than (lt
):
(df['High'].diff().gt(0)) & (df['High'].diff(-1).gt(0))
(df['Low'].diff().lt(0)) & (df['Low'].diff(-1).lt(0))
In you screenshot example this will only lead to a Series of False values.
You can also use greater or equal (ge
) or less or equal (le
), if you also want to return True if the second day has a value equal to the day before/after.
For example with High value on day 1986-03-17 which is equal to the next value of High on 1986-03-18. Or Low on 1986-03-24/1986-03-25.
This will create a True value for the mentioned Dates.