'Vectorized `cross` function in Python

Pinescript offers the cross function that will return True if two Series cross each other.

In python we could achieve the same easily for let's say two MA-s with a for loop and if statement. How can we vectorize this operation?

Specifically what is almost close to what I want:

d = pd.DataFrame({'a': [1, 1, 2, 2, 1, 1], 'b': [2, 2, 1, 1, 2, 2]})
np.where(d.a < d.b, True, False)
array([ True,  True, False, False,  True,  True])

But from my understanding in Pinescript we would have a signal only at the exact crossover point.

array([ False,  False, True, False,  True,  False])


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source