quantopian / alphalens

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

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

Closed PeterSikuda closed 5 years ago

PeterSikuda commented 5 years ago

Problem Description

I am trying to process factor, pricing data for alphalens analysis, but got this error.

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

al.utils.get_clean_factor_and_forward_returns(
        factor=df['sentiment'],
        prices=pricing,
        periods=(1,3))
pricing.index

DatetimeIndex(['2018-05-31', '2018-06-01', '2018-06-02', '2018-06-03', '2018-06-04', '2018-06-05', '2018-06-06', '2018-06-07', '2018-06-08', '2018-06-09', ... '2019-03-22', '2019-03-23', '2019-03-24', '2019-03-25', '2019-03-26', '2019-03-27', '2019-03-28', '2019-03-29', '2019-03-30', '2019-03-31'], dtype='datetime64[ns]', length=305, freq=None)

df.index.levels[0]

DatetimeIndex(['2018-05-31', '2018-06-01', '2018-06-02', '2018-06-03', '2018-06-04', '2018-06-05', '2018-06-06', '2018-06-07', '2018-06-08', '2018-06-09', ... '2019-03-22', '2019-03-23', '2019-03-24', '2019-03-25', '2019-03-26', '2019-03-27', '2019-03-28', '2019-03-29', '2019-03-30', '2019-03-31'], dtype='datetime64[ns]', name='datetime', length=305, freq=None)

Also tried:

pricing.index = pricing.index.values.astype('datetime64[D]')
df.index.levels[0].values.astype('datetime64[D]')

but didn't help.

Versions

luca-s commented 5 years ago

If you could upgrade Alphalens to version 0.3.6 that should fix the issue.

luca-s commented 5 years ago

Just to give more background information: we improved Alphalens compatibility with recent pandas versions (tested up to pandas 0.23.4) in one of the latest Alphalens releases. So it should worth upgrading Alphalens and see how it works.

PeterSikuda commented 5 years ago

thanks. upgrading to version 0.3.6 fixed the issue.