alphalens icon indicating copy to clipboard operation
alphalens copied to clipboard

ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Open xuanronaldo opened this issue 5 years ago • 6 comments

Problem Description

I cathed an exception as the title saying when I call the function "get_clean_factor_and_forward_returns()".But it worked well before. Please provide a minimal, self-contained, and reproducible example:

factor_return = get_clean_factor_and_forward_returns(multi_index_series, prices)

Please provide the full traceback:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-8d1ecc81ba64> in <module>
----> 1 factor_rice, IC_return_on_equity = alphalens_ic_calculate(fundamentals.financial_indicator.return_on_equity, start_date="2020-01-01", end_date="2020-08-20")

<ipython-input-6-86025a787884> in alphalens_ic_calculate(factor, start_date, end_date)
     30     prices = get_price(stock_list, start_date=start_date, end_date=end_date, fields='close')
     31 
---> 32     factor_return = get_clean_factor_and_forward_returns(multi_index_series, prices, zero_aware=True)
     33     return factor_returns(factor_return).iloc[:, 0].mean(), factor_information_coefficient(factor_return)
     34 

/usr/local/lib/python3.6/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, zero_aware, cumulative_returns)
    837                                    quantiles=quantiles, bins=bins,
    838                                    binning_by_group=binning_by_group,
--> 839                                    max_loss=max_loss, zero_aware=zero_aware)
    840 
    841     return factor_data

/usr/local/lib/python3.6/site-packages/alphalens/utils.py in get_clean_factor(factor, forward_returns, groupby, binning_by_group, quantiles, bins, groupby_labels, max_loss, zero_aware)
    593     factor_copy = factor.copy()
    594     factor_copy.index = factor_copy.index.rename(['date', 'asset'])
--> 595     factor_copy = factor_copy[np.isfinite(factor_copy)]
    596 
    597     merged_data = forward_returns.copy()

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Please provide any additional information below:

Versions

  • Alphalens version: 0.4.0
  • Python version: 3.6
  • Pandas version: 0.24.2
  • Matplotlib version: 3.1.3

xuanronaldo avatar Sep 07 '20 06:09 xuanronaldo

same as u...

OnlyAnJ avatar Sep 09 '20 00:09 OnlyAnJ

same as u...

I deleted this line,and it works now.

xuanronaldo avatar Sep 09 '20 03:09 xuanronaldo

same as u...

I deleted this line,and it works now.

so u donot use this function? But how do u get the best factor return

OnlyAnJ avatar Sep 09 '20 04:09 OnlyAnJ

same as u...

I deleted this line,and it works now.

so u donot use this function? But how do u get the best factor return

I do call this function! I mean this line "factor_copy = factor_copy[np.isfinite(factor_copy)]" was deleted.U can track this line by ctrl and left mouse button if u use pycharm or some IDE else.

xuanronaldo avatar Sep 09 '20 14:09 xuanronaldo

There is something wrong with your factor data dtype. Try astype ('float ')

syejing avatar Oct 19 '20 02:10 syejing

There is something wrong with your factor data dtype. Try astype ('float ')

Wow!It works.Thank you,dude.

xuanronaldo avatar Oct 19 '20 06:10 xuanronaldo