quantopian / alphalens

Performance analysis of predictive (alpha) stock factors
http://quantopian.github.io/alphalens
Apache License 2.0
3.2k stars 1.12k forks source link

Inferred frequency does not conform In Calculating TEARS (Pandas 0.23.4 issue) #319

Closed hexiaoling1994 closed 5 years ago

hexiaoling1994 commented 5 years ago

Here's the thing, I was dealing with a problem caused by pandas.datetimeindex.frequency

The api returned clean factor successfully, and I checked the .freq attribute, it seems in accordance with my input factor data:

default

when I ran:

alphalens.tears.create_returns_tear_sheet(factor_data, long_short=True, group_neutral=True, by_group=False)

I got the error:

F:\Anaconda3\lib\site-packages\alphalens-0.3.0-py3.6.egg\alphalens\tears.py in create_returns_tear_sheet(factor_data, long_short, group_neutral, by_group)
    316     factor_returns = perf.factor_returns(factor_data,
    317                                          long_short,
--> 318                                          group_neutral)
    319 
    320     mean_quant_ret, std_quantile = \

F:\Anaconda3\lib\site-packages\alphalens-0.3.0-py3.6.egg\alphalens\performance.py in factor_returns(factor_data, demeaned, group_adjust, equal_weight, by_asset)
    243 
    244     weights = \
--> 245         factor_weights(factor_data, demeaned, group_adjust, equal_weight)
    246 
    247     weighted_returns = \

F:\Anaconda3\lib\site-packages\alphalens-0.3.0-py3.6.egg\alphalens\performance.py in factor_weights(factor_data, demeaned, group_adjust, equal_weight)
    203 
    204     # preserve freq, which contains trading calendar information
--> 205     weights.index.levels[0].freq = factor_data.index.levels[0].freq
    206     return weights
    207 

F:\Anaconda3\lib\site-packages\pandas\core\indexes\datetimelike.py in freq(self, value)
    239         if value is not None:
    240             value = frequencies.to_offset(value)
--> 241             self._validate_frequency(self, value)
    242 
    243         self._freq = value

F:\Anaconda3\lib\site-packages\pandas\core\indexes\datetimelike.py in _validate_frequency(cls, index, freq, **kwargs)
    228             msg = ('Inferred frequency {infer} from passed values does not '
    229                    'conform to passed frequency {passed}')
--> 230             raise ValueError(msg.format(infer=inferred, passed=freq.freqstr))
    231 
    232     @property

ValueError: Inferred frequency None from passed values does not conform to passed frequency C

Seems the problem was caused by internal attribute weights. Any way I can fix this?

luca-s commented 5 years ago

Thank you for reporting this @hexiaoling1994.

luca-s commented 5 years ago

@hexiaoling1994 Could you also make sure you are using the latest Alphalens version 3.2?

>>> import alphalens
>>> alphalens.__version__
'0.3.2'
hexiaoling1994 commented 5 years ago

I checked the versions and updated numpy, pandas, alphalens to the latest.

And I tried:

factor_data = alphalens.utils.get_clean_factor_and_forward_returns(
            factor, prices, groupby, by_group=False, 
            quantiles=10, bins=None, periods=(2,5,8))

Another error happened, which also seems highly related to frequency of index:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-31-68a918e79fe3> in <module>()
      1 factor_data = alphalens.utils.get_clean_factor_and_forward_returns(
      2             factor, prices, groupby, by_group=False,
----> 3             quantiles=10, bins=None, periods=(2,5,8))

F:\Anaconda3\lib\site-packages\alphalens\utils.py in call_w_context(*args, **kwargs)
    609                           "replaced by 'binning_by_group'",
    610                           category=DeprecationWarning, stacklevel=3)
--> 611         return func(*args, **kwargs)
    612     return call_w_context
    613 

F:\Anaconda3\lib\site-packages\alphalens\utils.py in get_clean_factor_and_forward_returns(factor, prices, groupby, binning_by_group, quantiles, bins, periods, filter_zscore, groupby_labels, max_loss)
    762 
    763     forward_returns = compute_forward_returns(factor, prices, periods,
--> 764                                               filter_zscore)
    765 
    766     factor_data = get_clean_factor(factor, forward_returns, groupby=groupby,

F:\Anaconda3\lib\site-packages\alphalens\utils.py in compute_forward_returns(factor, prices, periods, filter_zscore)
    281             start = prices.index[p_idx]
    282             end = prices.index[p_idx + period]
--> 283             period_len = diff_custom_calendar_timedeltas(start, end, freq)
    284             days_diffs.append(period_len.components.days)
    285 

F:\Anaconda3\lib\site-packages\alphalens\utils.py in diff_custom_calendar_timedeltas(start, end, freq)
    920     if weekmask is not None and holidays is not None:
    921         # we prefer this method as it is faster
--> 922         actual_days = np.busday_count(start, end, weekmask, holidays)
    923     else:
    924         # default, it is slow

TypeError: Iterator operand 0 dtype could not be cast from dtype('<M8[us]') to dtype('<M8[D]') according to the rule 'safe'

Here's how the index looks like, I can't tell which part of it was wrong...

>>>factor.index.levels[0]

DatetimeIndex(['2016-01-04', '2016-01-05', '2016-01-06', '2016-01-07',
               '2016-01-08', '2016-01-11', '2016-01-12', '2016-01-13',
               '2016-01-14', '2016-01-15',
               ...
               '2018-08-13', '2018-08-14', '2018-08-15', '2018-08-16',
               '2018-08-17', '2018-08-20', '2018-08-21', '2018-08-22',
               '2018-08-23', '2018-08-24'],
              dtype='datetime64[ns]', name='DATES', length=690, freq='B')

the other inputs also seemed ok...

01 02 03

luca-s commented 5 years ago

I am closing this since no answer to my questions were provided. If you still have the issue feel free to re-open this but please answer those questions:

esahione commented 5 years ago

Hi,

I'm running into same exact issue. Getting it while running the simplest example notebook.

Pandas is 0.23.4, numpy is 1.15.3.

Cheers mate.

luca-s commented 5 years ago

I discovered the problem is Pandas version 0.23.4. In our tests we cover pandas 0.20.3 (see here ). Until we make Alphalens compatible with more recent versions of Pandas please stick with version 0.20.3