Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Time series with Python and Pandas

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()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here’s a piece of data.

enter image description here

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading