Beispiel #1
0
def overwrite_from_dates(asof, dense_dates, sparse_dates, asset_idx, value):
    """Construct an Overwrite with the correct
    start and end date based on the asof date of the delta,
    the dense_dates, and the dense_dates.

    Parameters
    ----------
    asof : datetime
        The asof date of the delta.
    dense_dates : pd.DatetimeIndex
        The dates requested by the loader.
    sparse_dates : pd.DatetimeIndex
        The dates that appeared in the dataset.
    asset_idx : tuple of int
        The index of the asset in the block. If this is a tuple, then this
        is treated as the first and last index to use.
    value : any
        The value to overwrite with.

    Returns
    -------
    overwrite : Float64Overwrite
        The overwrite that will apply the new value to the data.

    Notes
    -----
    This is forward-filling all dense dates that are between the asof_date date
    and the next sparse date after the asof_date.

    For example:
    let ``asof = pd.Timestamp('2014-01-02')``,
        ``dense_dates = pd.date_range('2014-01-01', '2014-01-05')``
        ``sparse_dates = pd.to_datetime(['2014-01', '2014-02', '2014-04'])``

    Then the overwrite will apply to indexes: 1, 2, 3, 4
    """
    if asof is pd.NaT:
        # Not an actual delta.
        # This happens due to the groupby we do on the deltas.
        return

    first_row = dense_dates.searchsorted(asof)
    next_idx = sparse_dates.searchsorted(asof.asm8, 'right')
    if next_idx == len(sparse_dates):
        # There is no next date in the sparse, this overwrite should apply
        # through the end of the dense dates.
        last_row = len(dense_dates) - 1
    else:
        # There is a next date in sparse dates. This means that the overwrite
        # should only apply until the index of this date in the dense dates.
        last_row = dense_dates.searchsorted(sparse_dates[next_idx]) - 1

    if first_row > last_row:
        return

    first, last = asset_idx
    yield make_adjustment_from_indices(
        first_row, last_row, first, last, OVERWRITE, value
    )
Beispiel #2
0
    def test_unsupported_type(self):
        class SomeClass(object):
            pass

        with self.assertRaises(TypeError) as e:
            adj.make_adjustment_from_indices(
                1, 2, 3, 4,
                adjustment_kind=adj.OVERWRITE,
                value=SomeClass(),
            )

        exc = e.exception
        expected_msg = (
            "Don't know how to make overwrite adjustments for values of type "
            "%r." % SomeClass
        )
        self.assertEqual(str(exc), expected_msg)
Beispiel #3
0
def overwrite_from_dates(asof, dense_dates, sparse_dates, asset_idx, value):
    """Construct an Overwrite with the correct
    start and end date based on the asof date of the delta,
    the dense_dates, and the dense_dates.

    Parameters
    ----------
    asof : datetime
        The asof date of the delta.
    dense_dates : pd.DatetimeIndex
        The dates requested by the loader.
    sparse_dates : pd.DatetimeIndex
        The dates that appeared in the dataset.
    asset_idx : tuple of int
        The index of the asset in the block. If this is a tuple, then this
        is treated as the first and last index to use.
    value : any
        The value to overwrite with.

    Returns
    -------
    overwrite : Float64Overwrite
        The overwrite that will apply the new value to the data.

    Notes
    -----
    This is forward-filling all dense dates that are between the asof_date date
    and the next sparse date after the asof_date.

    For example:
    let ``asof = pd.Timestamp('2014-01-02')``,
        ``dense_dates = pd.date_range('2014-01-01', '2014-01-05')``
        ``sparse_dates = pd.to_datetime(['2014-01', '2014-02', '2014-04'])``

    Then the overwrite will apply to indexes: 1, 2, 3, 4
    """
    if asof is pd.NaT:
        # Not an actual delta.
        # This happens due to the groupby we do on the deltas.
        return

    first_row = dense_dates.searchsorted(asof)
    next_idx = sparse_dates.searchsorted(asof.asm8, 'right')
    if next_idx == len(sparse_dates):
        # There is no next date in the sparse, this overwrite should apply
        # through the end of the dense dates.
        last_row = len(dense_dates) - 1
    else:
        # There is a next date in sparse dates. This means that the overwrite
        # should only apply until the index of this date in the dense dates.
        last_row = dense_dates.searchsorted(sparse_dates[next_idx]) - 1

    if first_row > last_row:
        return

    first, last = asset_idx
    yield make_adjustment_from_indices(first_row, last_row, first, last,
                                       OVERWRITE, value)
    def test_unsupported_type(self):
        class SomeClass(object):
            pass

        with self.assertRaises(TypeError) as e:
            adj.make_adjustment_from_indices(
                1,
                2,
                3,
                4,
                adjustment_kind=adj.OVERWRITE,
                value=SomeClass(),
            )

        exc = e.exception
        expected_msg = (
            "Don't know how to make overwrite adjustments for values of type "
            "%r." % SomeClass)
        self.assertEqual(str(exc), expected_msg)
Beispiel #5
0
 def test_make_datetime_adjustment(self):
     overwrite_dt = make_datetime64ns(0)
     result = adj.make_adjustment_from_indices(
         1, 2, 3, 4,
         adjustment_kind=adj.OVERWRITE,
         value=overwrite_dt,
     )
     expected = adj.Datetime64Overwrite(
         first_row=1,
         last_row=2,
         first_col=3,
         last_col=4,
         value=overwrite_dt,
     )
     self.assertEqual(result, expected)
 def test_make_int_adjustment(self):
     result = adj.make_adjustment_from_indices(
         1,
         2,
         3,
         4,
         adjustment_kind=adj.OVERWRITE,
         value=1,
     )
     expected = adj.Int64Overwrite(
         first_row=1,
         last_row=2,
         first_col=3,
         last_col=4,
         value=1,
     )
     self.assertEqual(result, expected)
 def test_make_datetime_adjustment(self):
     overwrite_dt = make_datetime64ns(0)
     result = adj.make_adjustment_from_indices(
         1,
         2,
         3,
         4,
         adjustment_kind=adj.OVERWRITE,
         value=overwrite_dt,
     )
     expected = adj.Datetime64Overwrite(
         first_row=1,
         last_row=2,
         first_col=3,
         last_col=4,
         value=overwrite_dt,
     )
     self.assertEqual(result, expected)
Beispiel #8
0
 def test_make_float_adjustment(self, name, adj_type):
     expected_types = {
         'add': adj.Float64Add,
         'multiply': adj.Float64Multiply,
         'overwrite': adj.Float64Overwrite,
     }
     result = adj.make_adjustment_from_indices(
         1, 2, 3, 4,
         adjustment_kind=adj_type,
         value=0.5,
     )
     expected = expected_types[name](
         first_row=1,
         last_row=2,
         first_col=3,
         last_col=4,
         value=0.5,
     )
     self.assertEqual(result, expected)
 def test_make_float_adjustment(self, name, adj_type):
     expected_types = {
         "add": adj.Float64Add,
         "multiply": adj.Float64Multiply,
         "overwrite": adj.Float64Overwrite,
     }
     result = adj.make_adjustment_from_indices(
         1,
         2,
         3,
         4,
         adjustment_kind=adj_type,
         value=0.5,
     )
     expected = expected_types[name](
         first_row=1,
         last_row=2,
         first_col=3,
         last_col=4,
         value=0.5,
     )
     self.assertEqual(result, expected)