Example #1
0
    def get_df_from_table(self, table_name, convert_dates=False):
        try:
            date_cols = self._datetime_int_cols[table_name]
        except KeyError:
            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
Example #2
0
    def get_df_from_table(self, table_name, convert_dates=False):
        try:
            date_cols = self._datetime_int_cols[table_name]
        except KeyError:
            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.
        kwargs = (
            {'parse_dates': {col: {'unit': 's'} for col in date_cols}}
            if convert_dates
            else {}
        )

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

        if not len(result):
            return empty_dataframe(*keysorted(dtypes))

        result.rename_axis(None, inplace=True)
        result = result[sorted(dtypes)]  # ensure expected order of columns
        return result