Repository: quantopian/zipline
File: tests/data/test_adjustments.py (Python)

Code before:
            drop=True,
        )
        assert_equal(dividend_payouts, expected_dividend_payouts)

        expected_dividend_ratios = pd.DataFrame(
            [[T(1), 0.95, 0],
             [T(2), 0.90, 1]],
            columns=['effective_date', 'ratio', 'sid'],
        )
        dividend_ratios = dividend_ratios.sort_values(
            ['effective_date', 'sid'],
        )
        dividend_ratios = dividend_ratios.reset_index(drop=True)
        assert_equal(dividend_ratios, expected_dividend_ratios)

        self.assertTrue(self.log_handler.has_warning(
            "Couldn't compute ratio for dividend sid=2, ex_date=1990-10-18,"
            " amount=10.000",
        ))
        self.assertTrue(self.log_handler.has_warning(
            "Couldn't compute ratio for dividend sid=2, ex_date=1990-10-19,"
            " amount=0.100",
        ))
        self.assertTrue(self.log_handler.has_warning(
            'Dividend ratio <= 0 for dividend sid=1, ex_date=1990-10-17,'
            ' amount=0.510',
        ))
        self.assertTrue(self.log_handler.has_warning(
            'Dividend ratio <= 0 for dividend sid=1, ex_date=1990-10-18,'
            ' amount=0.400',
        ))

    def _test_identity(self, name):
        sids = np.arange(5)
        dates = self.trading_calendar.all_sessions.tz_convert(None)

        def T(n):
            return dates[n]

        sort_key = ['effective_date', 'sid', 'ratio']
        input_ = pd.DataFrame(
            [[T(0), 0.1, 1],
             [T(1), 2.0, 1],
             [T(0), 0.1, 2],
             [T(4), 2.0, 2],
             [T(8), 2.4, 2]],
            columns=['effective_date', 'ratio', 'sid'],
        ).sort_values(sort_key)

        self.writer_without_pricing(dates, sids).write(**{name: input_})
        dfs = self.component_dataframes()

Review comment (bug):
For paranoia's sake, can we also check that we handle the case where this is well beyond the end of the pricing data (e.g., 10 days or something)? Since we're fiddling with indices in the code under test here, I could imagine a world where this worked for dates one past the end of the array, but failed if we were well after the end.

Code after:
        dividend_payouts = dividend_payouts.reset_index(drop=True)

        expected_dividend_payouts = dividend_payouts.sort_values(
            payout_sort_key,
        )
        expected_dividend_payouts = expected_dividend_payouts.reset_index(
            drop=True,
        )
        assert_equal(dividend_payouts, expected_dividend_payouts)

        expected_dividend_ratios = pd.DataFrame(
            [[T(1), 0.95, 0],
             [T(2), 0.90, 1]],
            columns=['effective_date', 'ratio', 'sid'],
        )
        dividend_ratios = dividend_ratios.sort_values(
            ['effective_date', 'sid'],
        )
        dividend_ratios = dividend_ratios.reset_index(drop=True)
        assert_equal(dividend_ratios, expected_dividend_ratios)

        self.assertTrue(self.log_handler.has_warning(
            "Couldn't compute ratio for dividend sid=2, ex_date=1990-10-18,"
            " amount=10.000",
        ))
        self.assertTrue(self.log_handler.has_warning(
            "Couldn't compute ratio for dividend sid=2, ex_date=1990-10-19,"
            " amount=0.100",
        ))
        self.assertTrue(self.log_handler.has_warning(
            "Couldn't compute ratio for dividend sid=2, ex_date=1990-11-01,"
            " amount=0.100",
        ))
        self.assertTrue(self.log_handler.has_warning(
            'Dividend ratio <= 0 for dividend sid=1, ex_date=1990-10-17,'
            ' amount=0.510',
        ))
        self.assertTrue(self.log_handler.has_warning(
            'Dividend ratio <= 0 for dividend sid=1, ex_date=1990-10-18,'
            ' amount=0.400',
        ))

    def _test_identity(self, name):
        sids = np.arange(5)
        dates = self.trading_calendar.all_sessions.tz_convert(None)

        def T(n):
            return dates[n]

        sort_key = ['effective_date', 'sid', 'ratio']
        input_ = pd.DataFrame(