Exemplo n.º 1
0
def test_find_base_date():
    data = ['2019-01-01', '2019-06-01', '2020-01-01',
            '2021-01-01'], [1, 1, 1, 1]
    cases = [
        '2017-02-01',
        '2018-02-01',
        '2018-02-01',
        '2019-02-01',
        '2019-05-15',
        '2020-02-01',
        '2021-02-01',
        '2022-02-01',
    ]
    expected = [
        ('2019-02-01', -2),
        ('2019-02-01', -1),
        ('2019-02-01', -1),
        ('2019-02-01', 0),
        ('2019-05-15', 0),
        ('2020-02-01', 0),
        ('2020-02-01', 1),
        ('2020-02-01', 2),
    ]
    received = [
        sc.DateNumericSeries(*data).find_base_date(d, 31, True) for d in cases
    ]
    assert received == expected
Exemplo n.º 2
0
def test_interpolate():
    data = ['2020-01-01', '2021-01-01',
            '2022-01-01'], [10, 130,
                            10], ['2021-04-01', '2021-07-01', '2021-10-01']
    cases = (
        {
            'how': 'linear'
        },
        {
            'how': 'spline'
        },
        {
            'how':
            'weighted',
            'weight_benchmark':
            sc.DateNumericSeries.from_items([('2020-{:02}-01'.format(m), m)
                                             for m in range(1, 13)] +
                                            [('2021-01-01', 13)], ),
        },
    )
    expected = [[100, 70, 40], [100, 70, 40], [10, 7, 4]]
    received = []
    for c in cases:
        series = sc.DateNumericSeries(*data[0:2])
        interpolation = series.interpolate(data[2], **c)
        received += [interpolation.map_values(int).get_values()]
    assert expected == received
Exemplo n.º 3
0
 def distance_for_date(self,
                       date: dt.Date,
                       take_abs: bool = True) -> DateNumeric:
     return sc.DateNumericSeries(
         self.get_dates(),
         self.date_series().map(lambda d: self.get_distance_func()
                                (date, d, take_abs)),
         sort_items=False,
         validate=False,
     )
Exemplo n.º 4
0
 def distance(self,
              d: [Native, dt.Date],
              take_abs: bool = True) -> DateNumeric:
     if dt.is_date(d):
         distance_series = self.distance_for_date(d, take_abs=take_abs)
     elif self._is_native(d):
         date_series = self.new(d, validate=False, sort_items=False)
         distance_series = sc.DateNumericSeries(
             self.get_dates(),
             self.date_series().map(
                 lambda i: date_series.get_distance_for_nearest_date(
                     i, take_abs)),
             sort_items=False,
             validate=False,
         )
     else:
         raise TypeError(
             'd-argument for distance-method must be date or DateSeries (got {}: {})'
             .format(type(d), d))
     return distance_series
Exemplo n.º 5
0
 def assume_date_numeric(self):
     return sc.DateNumericSeries(**self.get_props())
Exemplo n.º 6
0
 def assume_dates(self, validate=False):
     return sc.DateNumericSeries(validate=validate,
                                 **self._get_data_member_dict())