Skip to content

Commit

Permalink
Merge pull request #2426 from quantopian/update-adjustment-reader
Browse files Browse the repository at this point in the history
ENH: Expose SQLiteAdjustmentReader.get_df_from_table method.
  • Loading branch information
ernestoeperez88 authored Feb 19, 2019
2 parents 660b4e8 + d8cfa15 commit 6110ce3
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions zipline/data/adjustments.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,33 +197,39 @@ def unpack_db_to_component_dfs(self, convert_dates=False):
version of the table, where all date columns have been coerced back
from int to datetime.
"""
return {
t_name: self.get_df_from_table(t_name, convert_dates)
for t_name in self._datetime_int_cols
}

def _get_df_from_table(table_name, date_cols):

# 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 {}
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(),
)
)

return pd.read_sql(
'select * from "{}"'.format(table_name),
self.conn,
index_col='index',
**kwargs
).rename_axis(None)
# 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 {}
)

return {
t_name: _get_df_from_table(
t_name,
date_cols
)
for t_name, date_cols in self._datetime_int_cols.items()
}
return pd.read_sql(
'select * from "{}"'.format(table_name),
self.conn,
index_col='index',
**kwargs
).rename_axis(None)


class SQLiteAdjustmentWriter(object):
Expand Down

0 comments on commit 6110ce3

Please sign in to comment.