Reputation: 877
This is my DataFrame
Date Time Value
16.02.2020 21:00:00 0.05012
16.02.2020 22:00:00 0.04285
16.02.2020 23:00:00 0.03559
17.02.2020 0:00:00 0.02833
17.02.2020 1:00:00 0.02107
17.02.2020 2:00:00 0.01380
17.02.2020 3:00:00 0.00654
17.02.2020 4:00:00 -0.00073
17.02.2020 5:00:00 -0.00799
17.02.2020 6:00:00 -0.01526
17.02.2020 7:00:00 -0.02252
17.02.2020 8:00:00 -0.02978
17.02.2020 9:00:00 -0.03704
17.02.2020 10:00:00 -0.04430
17.02.2020 11:00:00 -0.05156
How to find rows where Value crosses Zero? I can do it with iterating:
if current value > 0 and previous value < 0,
or
current value < 0 and previous value > 0,
so it is zero-crossing.
But my dataframe is very large. How to do it without iterating?
Upvotes: 4
Views: 1314
Reputation: 2811
we can create masks to make this filter with shift
mask1 = (df['Value'] < 0)
mask2 = (df['Value'] > 0).shift()
mask3 = (df['Value'] > 0)
mask4 = (df['Value'] < 0).shift()
df.loc[(mask1 & mask2) | (mask3 & mask4)]
#output:
Date Time Value
7 17.02.2020 4:00:00 -0.00073
Upvotes: 2
Reputation: 75080
IIUC, you can try np.sign
+ series.diff
out = df[np.sign(df['Value']).diff().fillna(0).ne(0)].copy()
Upvotes: 4