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

Code before:

    def in_memory_reader_for_close(self, close):
        nan_frame = pd.DataFrame(
            np.nan,
            index=close.index,
            columns=close.columns,
        )
        frames = {'close': close}
        for key in 'open', 'high', 'low', 'volume':
            frames[key] = nan_frame
        return InMemoryDailyBarReader(frames, self.trading_calendar)

    def writer_from_close(self, close):
        return self.writer(self.in_memory_reader_for_close(close))

    def assert_all_empty(self, dfs):
        for k, v in dfs.items():
            assert_equal(len(v), 0, msg='%s dataframe should be empty' % k)

    def test_calculate_dividend_ratio(self):
        first_date_ix = 200
        dates = self.trading_calendar.all_sessions[
            first_date_ix:first_date_ix + 3
        ]

        one_day_past_pricing_data = self.trading_calendar.all_sessions[
            first_date_ix + 3
        ].tz_convert(None)

        def T(n):
            return dates[n].tz_convert(None)

        close = pd.DataFrame(
            [[10.0, 0.5,   30.0],   # noqa
             [ 9.5, 0.4, np.nan],   # noqa
             [15.0, 0.6,   np.nan]],  # noqa
            columns=[0, 1, 2],
            index=dates,
        )

        dividends = pd.DataFrame(
            [
                # ex_date of 0 means that we cannot get the previous day's
                # close, so we should not expect to see this dividend in the
                # output
                [0, T(0), 10],

                # previous price was 0.4, meaning the dividend amount
                # is greater than or equal to price and the ratio would be
                # negative. we should warn and drop this row
                [1, T(1), 0.51],

Review comment (suggestion):
`dates[-1] + self.trading_calendar.day` might be a more direct expression of the intent here. That will also continue to work if we need to extend the length of `dates` in this test.

Code after:

    def in_memory_reader_for_close(self, close):
        nan_frame = pd.DataFrame(
            np.nan,
            index=close.index,
            columns=close.columns,
        )
        frames = {'close': close}
        for key in 'open', 'high', 'low', 'volume':
            frames[key] = nan_frame
        return InMemoryDailyBarReader(frames, self.trading_calendar)

    def writer_from_close(self, close):
        return self.writer(self.in_memory_reader_for_close(close))

    def assert_all_empty(self, dfs):
        for k, v in dfs.items():
            assert_equal(len(v), 0, msg='%s dataframe should be empty' % k)

    def test_calculate_dividend_ratio(self):
        first_date_ix = 200
        dates = self.trading_calendar.all_sessions[
            first_date_ix:first_date_ix + 3
        ]

        before_pricing_data = \
            (dates[0] - self.trading_calendar.day).tz_convert(None)
        one_day_past_pricing_data = \
            (dates[-1] + self.trading_calendar.day).tz_convert(None)
        ten_days_past_pricing_data = \
            (dates[-1] + self.trading_calendar.day * 10).tz_convert(None)

        def T(n):
            return dates[n].tz_convert(None)

        close = pd.DataFrame(
            [[10.0, 0.5,   30.0],   # noqa
             [ 9.5, 0.4, np.nan],   # noqa
             [15.0, 0.6,   np.nan]],  # noqa
            columns=[0, 1, 2],
            index=dates,
        )

        dividends = pd.DataFrame(
            [
                # ex_date of >=0 means that we cannot get the previous day's
                # close, so we should not expect to see this dividend in the
                # output
                [0, before_pricing_data, 10],
                [0, T(0), 10],