Indexing/Filtering Pairs#

import pandas as pd

import faninsar as fis
dates = pd.date_range(start="2020-01-01",end="2024-12-31",freq="12D")
dates
DatetimeIndex(['2020-01-01', '2020-01-13', '2020-01-25', '2020-02-06',
               '2020-02-18', '2020-03-01', '2020-03-13', '2020-03-25',
               '2020-04-06', '2020-04-18',
               ...
               '2024-09-12', '2024-09-24', '2024-10-06', '2024-10-18',
               '2024-10-30', '2024-11-11', '2024-11-23', '2024-12-05',
               '2024-12-17', '2024-12-29'],
              dtype='datetime64[ns]', length=153, freq='12D')
pairs_factory = fis.PairsFactory(dates)
pairs = pairs_factory.from_interval(max_interval=3)
pairs
         Pairs           
       primary  secondary
0   2020-01-01 2020-01-13
1   2020-01-01 2020-01-25
2   2020-01-01 2020-02-06
3   2020-01-13 2020-01-25
4   2020-01-13 2020-02-06
..         ...        ...
448 2024-11-23 2024-12-17
449 2024-11-23 2024-12-29
450 2024-12-05 2024-12-17
451 2024-12-05 2024-12-29
452 2024-12-17 2024-12-29

[453 rows x 2 columns]

Filter Pairs date range#

If you want to filter pairs using the start and end dates, you can directly slice the pairs just like you would slice a pandas datetime index.

pairs["2022":"2024-03-15"]
         Pairs           
       primary  secondary
0   2022-01-02 2022-01-14
1   2022-01-02 2022-01-26
2   2022-01-02 2022-02-07
3   2022-01-14 2022-01-26
4   2022-01-14 2022-02-07
..         ...        ...
190 2024-01-28 2024-02-21
191 2024-01-28 2024-03-04
192 2024-02-09 2024-02-21
193 2024-02-09 2024-03-04
194 2024-02-21 2024-03-04

[195 rows x 2 columns]

Filter Pairs by custom condition#

For example, if you want to keep only pairs that month of primary dates are in [8,9], you can achieve this by using the following code:

mask = pairs.primary.month.map(lambda x: x in [8, 9])
pairs[mask]
        Pairs           
      primary  secondary
0  2020-08-04 2020-08-16
1  2020-08-04 2020-08-28
2  2020-08-04 2020-09-09
3  2020-08-16 2020-08-28
4  2020-08-16 2020-09-09
..        ...        ...
73 2024-09-12 2024-10-06
74 2024-09-12 2024-10-18
75 2024-09-24 2024-10-06
76 2024-09-24 2024-10-18
77 2024-09-24 2024-10-30

[78 rows x 2 columns]