Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect sign changes in Pandas Dataframe

Tags:

python

pandas

I have a pandas dataframe that is datetime indexed and it looks like this:

Datetime
2020-05-11 14:00:00-03:00    0.097538
2020-05-11 14:30:00-03:00   -0.083788
2020-05-11 15:00:00-03:00   -0.074128
2020-05-11 15:30:00-03:00    0.059725
2020-05-11 16:00:00-03:00    0.041369
2020-05-11 16:30:00-03:00    0.034388
2020-05-12 10:00:00-03:00    0.006814
2020-05-12 10:30:00-03:00   -0.005308
2020-05-12 11:00:00-03:00   -0.036952
2020-05-12 11:30:00-03:00   -0.070307
2020-05-12 12:00:00-03:00    0.102004
2020-05-12 12:30:00-03:00   -0.139317
2020-05-12 13:00:00-03:00   -0.167589
2020-05-12 13:30:00-03:00   -0.179942
2020-05-12 14:00:00-03:00    0.182351
2020-05-12 14:30:00-03:00   -0.160736
2020-05-12 15:00:00-03:00   -0.150033
2020-05-12 15:30:00-03:00   -0.141862
2020-05-12 16:00:00-03:00   -0.121372
2020-05-12 16:30:00-03:00   -0.095990
Name: result_col, dtype: float64

My need is to mark the rows where it changes signal, from negative to positive and vice-versa. Any thoughts on how to achieve it?

Edit: I need +1 on the cross up and -1 on the cross down.

like image 709
Carlos Cariello Avatar asked Dec 08 '22 10:12

Carlos Cariello


1 Answers

Let us try

import numpy as np 
np.sign(data).diff().ne(0)
like image 129
BENY Avatar answered Dec 29 '22 20:12

BENY