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

Code before:
        Returns
        -------
        list of np.ndarray
            A list with an entry per field of ndarrays with shape
            (minutes in range, sids) with a dtype of float64, containing the
            values for the respective field over start and end dt range.
        """
        self._validate_timestamp(start_date)
        self._validate_timestamp(end_date)

        start = start_date.asm8
        end = end_date.asm8

        sid_selector, out_buf_indexer = self._make_sid_indexers(assets)

        date_slice = self._compute_date_range_slice(start, end)

        n_dates = date_slice.stop - date_slice.start
        n_valid_sids = len(self.sids)
        n_query_sids = len(assets)

        read_buf = np.zeros((n_valid_sids, n_dates), dtype=np.uint32)

        out = []
        for column in columns:
            # Zero the buffer to prepare it to receive new data.
            read_buf.fill(0)

            dataset = self._country_group[DATA][column]

            dataset.read_direct(
                read_buf,
                np.s_[:, date_slice],
            )

            out_buf = np.zeros((n_query_sids, n_dates), dtype=np.uint32)
            out_buf[out_buf_indexer] = read_buf[sid_selector]

            out.append(
                self._postprocessors[column](
                    out_buf.T
                )
            )

        return out

    def _make_sid_indexers(self, assets):
        """
        Given the assets that have been queried, returns two indexers:

          (1) Indexer to select the requested sids from the data read

Review comment (suggestion):
```suggestion
        # pull from the h5 file) into an array aligned to ``assets``.
```

Code after:
        Returns
        -------
        list of np.ndarray
            A list with an entry per field of ndarrays with shape
            (minutes in range, sids) with a dtype of float64, containing the
            values for the respective field over start and end dt range.
        """
        self._validate_timestamp(start_date)
        self._validate_timestamp(end_date)

        start = start_date.asm8
        end = end_date.asm8
        date_slice = self._compute_date_range_slice(start, end)
        n_dates = date_slice.stop - date_slice.start

        # Create a buffer into which we'll read data from the h5 file.
        # Allocate an extra row of space that will always contain null values.
        # We'll use that space to provide "data" for entries in ``assets`` that
        # are unknown to us.
        full_buf = np.zeros((len(self.sids) + 1, n_dates), dtype=np.uint32)

        # We'll only read values into this portion of the read buf.
        mutable_buf = full_buf[:-1]

        # Indexer that converts an array aligned to self.sids (which is what we
        # pull from the h5 file) into an array aligned to assets.
        #
        # Unknown assets will have an index of -1, which means they'll always
        # pull from the last row of the read buffer. We allocated an extra
        # empty row above so that these lookups will cause us to fill our
        # output buffer with "null" values.
        sid_selector = self._make_sid_indexers(assets)

        out = []
        for column in columns:
            # Zero the buffer to prepare to receive new data.
            mutable_buf.fill(0)

            dataset = self._country_group[DATA][column]

            # Fill the mutable portion of our buffer with data from the file.
            dataset.read_direct(
                mutable_buf,
                np.s_[:, date_slice.start:date_slice.stop],
            )

            # Select data from the **full buffer**. Unknown assets will pull
            # from the last row, which is always empty.
            out.append(self._postprocessors[column](full_buf[sid_selector].T))

        return out