Skip to content

Commit 463fad1

Browse files
committed
Pass freq where necessary, remove forcing day-only logic so that e.g. 4h data will also work
1 parent 597f4c4 commit 463fad1

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

alphalens/performance.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def to_weights(group, _demeaned, _equal_weight):
201201

202202
if group_adjust:
203203
weights = weights.groupby(level='date').apply(to_weights, False, False)
204-
204+
weights.index.freq = factor_data.index.levels[0].freq
205205
return weights
206206

207207

@@ -251,6 +251,7 @@ def factor_returns(factor_data,
251251
returns = weighted_returns
252252
else:
253253
returns = weighted_returns.groupby(level='date').sum()
254+
returns.index.freq = factor_data.index.levels[0].freq
254255

255256
return returns
256257

@@ -384,8 +385,6 @@ def positions(weights, period, freq=None):
384385
2004-01-13 10:30:00 -13853.2800 13653.6400 -43.6375
385386
"""
386387

387-
weights = weights.unstack()
388-
389388
if not isinstance(period, pd.Timedelta):
390389
period = pd.Timedelta(period)
391390

@@ -394,6 +393,7 @@ def positions(weights, period, freq=None):
394393

395394
if freq is None:
396395
raise Exception("'freq' not set")
396+
weights = weights.unstack()
397397

398398
#
399399
# weights index contains factor computation timestamps, then add returns

alphalens/utils.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -984,8 +984,6 @@ def add_custom_calendar_timedelta(input, timedelta, freq):
984984
pd.DatetimeIndex or pd.Timestamp
985985
input + timedelta
986986
"""
987-
if not isinstance(freq, (Day, BusinessDay, CustomBusinessDay)):
988-
raise ValueError("freq must be Day, BDay or CustomBusinessDay")
989987
days = timedelta.components.days
990988
offset = timedelta - pd.Timedelta(days=days)
991989
return input + freq * days + offset
@@ -1010,7 +1008,7 @@ def diff_custom_calendar_timedeltas(start, end, freq):
10101008
end - start
10111009
"""
10121010
if not isinstance(freq, (Day, BusinessDay, CustomBusinessDay)):
1013-
raise ValueError("freq must be Day, BusinessDay or CustomBusinessDay")
1011+
return end - start
10141012

10151013
weekmask = getattr(freq, 'weekmask', None)
10161014
holidays = getattr(freq, 'holidays', None)

0 commit comments

Comments
 (0)