title: Pandas: determine where 2 time series are intersecting and in which direction
body: <p>This is a follow up to this question: <a href="https://stackoverflow.com/questions/33707150/determine-the-coordinates-where-two-pandas-time-series-cross-and-how-many-times">determine the coordinates where two pandas time series cross, and how many times the time series cross</a></p>
  
  <p>I have 2 series in my Pandas dataframe, and would like to know where they intersect.</p>
  
  <pre><code>   A    B
  0  1  0.5
  1  2  3.0
  2  3  1.0
  3  4  1.0
  4  5  6.0
  </code></pre>
  
  <p>With this code, we can create a third column that will contain True everytime the two series intersect:</p>
  
  <pre><code>df['difference'] = df.A - df.B
  
  df['cross'] = np.sign(df.difference.shift(1))!=np.sign(df.difference)
  np.sum(df.cross)-1
  </code></pre>
  
  <p>Now, instead of a simple True or False, I would to know in which direction the intersection took place. For example: from 1 to 2, it intersected upwards, from 2 to 3 downwards, from 3 to 4 no intersections, from 4 to 5 upwards.</p>
  
  <p><a href="https://i.stack.imgur.com/myW0N.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/myW0N.png" alt="enter image description here"></a></p>
  
  <pre><code>   A    B  Cross_direction
  0  1  0.5  None
  1  2  3.0  Upwards
  2  3  1.0  Downwards
  3  4  1.0  None
  4  5  6.0  Upwards
  </code></pre>
  
  <p>In pseudo-code, it should be like this:</p>
  
  <pre><code>cross_directions = [none, none, ... * series size]
  for item in df['difference']:
      if item &gt; 0 and next_item &lt; 0:
          cross_directions.append("up")
      elif item &lt; 0 and next_item &gt; 0:
          cross_directions.append("down")
  </code></pre>
  
  <p>The problem is that <code>next_item</code> is unavailable with this syntax (we obtain that in the original syntax using <code>.shift(1)</code>) and that it takes a lot of code.</p>
  
  <p>Should I look into implementing the code above using something that can group the loop by 2 items at a time? Or is there a simpler and more elegant solution like the one from the previous question?</p>