Handling wrapping of values in pandas
<p>I am working with data coming from a position sensor that reports in angular coordinates (pitch, roll, and yaw).  The sensors readout is such that the maximum reading is 180 degrees.  Going past that, the readings jump to negative degrees.  For example, a slightly oscillating series of readings might go something like:</p>

<pre><code> 178.8
 178.9
-178.8
-178.0
-179.0
 180.0
 179.0
</code></pre>

<p>I'm presently handling this by checking all the values sequentially in a loop.  I get the delta between two sequential readings and keep a running total to get my current angular position.  If I cross from the 3rd quadrant (90 &lt;= previous value &lt;= 180) into the fourth (-180 &lt;= present value &lt;= -90), I add 360 to the present value before taking the delta.  If I'm going the fourth quadrant into the 3rd, I subtract 360 before taking the delta.  For example:</p>

<pre><code>    Yaw = inData['Yaw']
    cum_sum = Yaw[0]
    for i in range(1, Yaw)):
        x_prev = Yaw[i-1]
        x = Yaw[i]
        if 90 &lt;= x_prev and x_prev &lt;= 180 and -180 &lt;= x and x &lt;= -90:
            x = x + 360
        elif -180 &lt;= x_prev and x_prev &lt;= -90 and 90 &lt;= x and x &lt;= 180:
            x = x - 360
        else:
            pass
        delta = x - x_prev
        cum_sum += delta
</code></pre>

<p>Here, inData is a dataframe containing angular positions from multiple axes, and 'Yaw' is a series in that dataframe.  </p>

<p>If I could do a consistent correction to the angle values, I could generate a series of deltas and do a cumulative sum to get position.  However, adding 360 to all readings or working with the angles as modulo 360 only moves the roll-over point from crossing between +/-180 to crossing between 0 and 360.  </p>

<p>Given that I already have these readings in a DataFrame/Series, I was wondering if there is a more Pythonic (and faster) way to do this.</p>