Search code examples
pythonpandasscipy

In search of retrograde Mercury with pandas


My object, the planet Mercury, is moving in a circle back and forth. Its coordinate lies within 360 degrees. There is no problem in detecting its reversal if it is far from the 360 degree point (0 degrees).

an easy case:
13.08.2010  166.41245
14.08.2010  167.00584
15.08.2010  167.53165
16.08.2010  167.98625
17.08.2010  168.36589
18.08.2010  168.66672
19.08.2010  168.88494
20.08.2010  169.01682
21.08.2010  169.05885 This is where the backward movement begins
22.08.2010  169.00792 I detect it easily
23.08.2010  168.86147
24.08.2010  168.61771
25.08.2010  168.27591
26.08.2010  167.83665

I do this by searching for extremes.

from scipy.signal import argrelextrema

But if it goes from 359 degrees to 1 degree, I get constant crashes.

Here is an example where the system fails. It considers it is a reversal of Mercury, although it just goes from 359 degrees to the 0 degree. This is not the beginning of retrogression.

crash example
13.03.2010  350.60172
14.03.2010  352.53184
15.03.2010  354.47785
16.03.2010  356.43861
17.03.2010  358.41273 This is not the beginning of a backward movement (NOT MAXIMUM)
18.03.2010    0.39843 its just ingression from Pieces to Aries
19.03.2010    2.39354 
20.03.2010    4.39545
21.03.2010    6.40106
22.03.2010    8.40673
23.03.2010   10.40828
24.03.2010   12.40098
25.03.2010   14.37956
26.03.2010   16.33824

So, I need desicion to find reversal points of a planet that moves on a circle 360 degrees.


Solution

  • IIUC, you can use a threshold to avoid false positive:

    c = df['Coords']
    m0 = c.diff().abs().le(1)  # limit the gap, threshold=1
    m1 = c.gt(c.shift(-1)) & c.gt(c.shift()) & m0  # upper peak ↗↘
    m2 = c.lt(c.shift(-1)) & c.lt(c.shift()) & m0  # lower peak ↘↗
    df['Reversal'] = m1|m2
    

    Output:

    >>> df
              Date     Coords  Reversal
    0   13.03.2010  350.60172     False
    1   14.03.2010  352.53184     False
    2   15.03.2010  354.47785     False
    3   16.03.2010  356.43861     False
    4   17.03.2010  358.41273     False
    5   18.03.2010    0.39843     False  # ignore this case
    6   19.03.2010    2.39354     False
    7   20.03.2010    4.39545     False
    8   21.03.2010    6.40106     False
    9   22.03.2010    8.40673     False
    10  23.03.2010   10.40828     False
    11  24.03.2010   12.40098     False
    12  25.03.2010   14.37956     False
    13  26.03.2010   16.33824     False
    14  13.08.2010  166.41245     False
    15  14.08.2010  167.00584     False
    16  15.08.2010  167.53165     False
    17  16.08.2010  167.98625     False
    18  17.08.2010  168.36589     False
    19  18.08.2010  168.66672     False
    20  19.08.2010  168.88494     False
    21  20.08.2010  169.01682     False
    22  21.08.2010  169.05885      True  # real case
    23  22.08.2010  169.00792     False
    24  23.08.2010  168.86147     False
    25  24.08.2010  168.61771     False
    26  25.08.2010  168.27591     False
    27  26.08.2010  167.83665     False