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

Code before:
        result = pd.read_sql(
            'select * from "{}"'.format(table_name),
            self.conn,
            index_col='index',
            **kwargs
        ).rename_axis(None)

        if not len(result):
            dtypes = self._df_dtypes(table_name, convert_dates)
            return empty_dataframe(*keysorted(dtypes))

        return result

    def _df_dtypes(self, table_name, convert_dates):
        """Get dtypes to use when unpacking sqlite tables as dataframes.
        """
        out = self._raw_table_dtypes[table_name]
        if convert_dates:
            out = out.copy()
            for date_column in self._datetime_int_cols[table_name]:
                out[date_column] = datetime64ns_dtype

        return out

class SQLiteAdjustmentWriter(object):
    """
    Writer for data to be read by SQLiteAdjustmentReader

    Parameters
    ----------
    conn_or_path : str or sqlite3.Connection
        A handle to the target sqlite database.
    equity_daily_bar_reader : SessionBarReader
        Daily bar reader to use for dividend writes.
    overwrite : bool, optional, default=False
        If True and conn_or_path is a string, remove any existing files at the
        given path before connecting.

    See Also
    --------
    zipline.data.adjustments.SQLiteAdjustmentReader
    """

    def __init__(self, conn_or_path, equity_daily_bar_reader, overwrite=False):
        if isinstance(conn_or_path, sqlite3.Connection):
            self.conn = conn_or_path
        elif isinstance(conn_or_path, six.string_types):
            if overwrite:
                try:
                    remove(conn_or_path)
                except OSError as e:

Review comment (suggestion):
TIL `len(result)` is faster than `result.empty`. It seems `result.empty` performs extra checks we might not care about in this case.

Slightly faster, we could do `len(result.index)` instead.



Code after:
            raise ValueError(
                "Requested table %s not found.\n"
                "Available tables: %s\n" % (
                    table_name,
                    self._datetime_int_cols.keys(),
                )
            )

        # Dates are stored in second resolution as ints in adj.db tables.
        # Need to specifically convert them as UTC, not local time.
        kwargs = (
            {'parse_dates': {col: {'unit': 's', 'utc': True}
                             for col in date_cols}
             }
            if convert_dates
            else {}
        )

        result = pd.read_sql(
            'select * from "{}"'.format(table_name),
            self.conn,
            index_col='index',
            **kwargs
        ).rename_axis(None)

        if not len(result):
            dtypes = self._df_dtypes(table_name, convert_dates)
            return empty_dataframe(*keysorted(dtypes))

        return result

    def _df_dtypes(self, table_name, convert_dates):
        """Get dtypes to use when unpacking sqlite tables as dataframes.
        """
        out = self._raw_table_dtypes[table_name]
        if convert_dates:
            out = out.copy()
            for date_column in self._datetime_int_cols[table_name]:
                out[date_column] = datetime64ns_dtype

        return out


class SQLiteAdjustmentWriter(object):
    """
    Writer for data to be read by SQLiteAdjustmentReader

    Parameters
    ----------
    conn_or_path : str or sqlite3.Connection
        A handle to the target sqlite database.