Skip to content

Commit be0ecb6

Browse files
author
Andrew Daniels
authored
PERF: Replace get_loc calls in calc_dividend_ratios with get_indexer (#1510)
We can make a single vectorized call outside of the loop, instead of repeatedly calling get_loc inside it.
1 parent ac9d841 commit be0ecb6

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

zipline/data/us_equity_pricing.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ def calc_dividend_ratios(self, dividends):
959959
- effective_date, the date in seconds on which to apply the ratio.
960960
- ratio, the ratio to apply to backwards looking pricing data.
961961
"""
962-
if dividends is None:
962+
if dividends is None or dividends.empty:
963963
return DataFrame(np.array(
964964
[],
965965
dtype=[
@@ -978,12 +978,26 @@ def calc_dividend_ratios(self, dividends):
978978
equity_daily_bar_reader = self._equity_daily_bar_reader
979979

980980
effective_dates = full(len(amounts), -1, dtype=int64)
981+
981982
calendar = self._calendar
983+
984+
# Calculate locs against a tz-naive cal, as the ex_dates are tz-
985+
# naive.
986+
#
987+
# TODO: A better approach here would be to localize ex_date to
988+
# the tz of the calendar, but currently get_indexer does not
989+
# preserve tz of the target when method='bfill', which throws
990+
# off the comparison.
991+
tz_naive_calendar = calendar.tz_localize(None)
992+
day_locs = tz_naive_calendar.get_indexer(ex_dates, method='bfill')
993+
982994
for i, amount in enumerate(amounts):
983995
sid = sids[i]
984996
ex_date = ex_dates[i]
985-
day_loc = calendar.get_loc(ex_date, method='bfill')
997+
day_loc = day_locs[i]
998+
986999
prev_close_date = calendar[day_loc - 1]
1000+
9871001
try:
9881002
prev_close = equity_daily_bar_reader.get_value(
9891003
sid, prev_close_date, 'close')

0 commit comments

Comments
 (0)