Open CptanPanic opened 6 years ago
I'm having the exact same problem (discrepancy of exactly 1), but with a different size frame:
ValueError: Shape of passed values is (1, 13011), indices imply (0, 13010)
Did you have any luck fixing this, or anybody else have an idea?
@Granger7 no I haven't figured it out, and it makes pyfolio unusable for me. Was hoping someone familiar with the code would have jumped in by now.
Hi @CptanPanic, just a guess, but are you on the latest version of empyrical
? There've been quite a few changes lately, so that might be the problem. If not, @twiecki should be able to help: the stack trace leads back to empyrical
, which might be worrying.
I am getting a similar error. Empyrical and all other dependencies are up to date. Return and benchmark series have definitely the same length. Traces back to sklearn. Any solutions to this issue?
ValueError Traceback (most recent call last)
<ipython-input-280-574b51b7a8af> in <module>()
2 #benchmark_rets=test['returns'],
3
----> 4 pf.create_returns_tear_sheet(test['dnn_returns'], benchmark_rets=test['returns'], live_start_date=livestart)
~/anaconda/lib/python3.6/site-packages/pyfolio/plotting.py in call_w_context(*args, **kwargs)
50 if set_context:
51 with plotting_context(), axes_style():
---> 52 return func(*args, **kwargs)
53 else:
54 return func(*args, **kwargs)
~/anaconda/lib/python3.6/site-packages/pyfolio/tears.py in create_returns_tear_sheet(returns, positions, transactions, live_start_date, cone_std, benchmark_rets, bootstrap, return_fig)
551
552 plotting.plot_rolling_fama_french(
--> 553 returns, ax=ax_rolling_risk)
554
555 # Drawdowns
~/anaconda/lib/python3.6/site-packages/pyfolio/plotting.py in plot_rolling_fama_french(returns, factor_returns, rolling_window, legend_loc, ax, **kwargs)
188 returns,
189 factor_returns=factor_returns,
--> 190 rolling_window=rolling_window)
191
192 rolling_beta.plot(alpha=0.7, ax=ax, **kwargs)
~/anaconda/lib/python3.6/site-packages/pyfolio/timeseries.py in rolling_fama_french(returns, factor_returns, rolling_window)
591 factor_returns.index[rolling_window:]):
592 coeffs = linear_model.LinearRegression().fit(factor_returns[beg:end],
--> 593 returns[beg:end]).coef_
594 regression_coeffs = np.append(regression_coeffs, [coeffs], axis=0)
595
~/anaconda/lib/python3.6/site-packages/sklearn/linear_model/base.py in fit(self, X, y, sample_weight)
480 n_jobs_ = self.n_jobs
481 X, y = check_X_y(X, y, accept_sparse=['csr', 'csc', 'coo'],
--> 482 y_numeric=True, multi_output=True)
483
484 if sample_weight is not None and np.atleast_1d(sample_weight).ndim > 1:
~/anaconda/lib/python3.6/site-packages/sklearn/utils/validation.py in check_X_y(X, y, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
581 y = y.astype(np.float64)
582
--> 583 check_consistent_length(X, y)
584
585 return X, y
~/anaconda/lib/python3.6/site-packages/sklearn/utils/validation.py in check_consistent_length(*arrays)
202 if len(uniques) > 1:
203 raise ValueError("Found input variables with inconsistent numbers of"
--> 204 " samples: %r" % [int(l) for l in lengths])
205
206
ValueError: Found input variables with inconsistent numbers of samples: [127, 131]
@CptanPanic Looks like you're passing in a dataframe where a Series is expected.
@j220 This should be fixed on master, can you update via: pip install -U --no-deps git+https://github.com/quantopian/alphalens
Thanks for the help @twiecki. I updated but the error is still the same.
Oops, I had an error above, should be: pip install -U --no-deps git+https://github.com/quantopian/pyfolio
Now the error changes: ValueError: Input contains NaN, infinity or a value too large for dtype('float64').
Can you post the full traceback?
ValueError Traceback (most recent call last)
<ipython-input-92-df699bf1d804> in <module>()
2 livestart='2017-01-01'
3 #benchmark_rets=test['returns'],
----> 4 pf.create_returns_tear_sheet(test['dnn_returns'], benchmark_rets=test['returns'], live_start_date=livestart)
~/anaconda/lib/python3.6/site-packages/pyfolio/plotting.py in call_w_context(*args, **kwargs)
50 if set_context:
51 with plotting_context(), axes_style():
---> 52 return func(*args, **kwargs)
53 else:
54 return func(*args, **kwargs)
~/anaconda/lib/python3.6/site-packages/pyfolio/tears.py in create_returns_tear_sheet(returns, positions, transactions, live_start_date, cone_std, benchmark_rets, bootstrap, turnover_denom, header_rows, return_fig)
590
591 plotting.plot_rolling_fama_french(
--> 592 returns, ax=ax_rolling_risk)
593
594 # Drawdowns
~/anaconda/lib/python3.6/site-packages/pyfolio/plotting.py in plot_rolling_fama_french(returns, factor_returns, rolling_window, legend_loc, ax, **kwargs)
188 returns,
189 factor_returns=factor_returns,
--> 190 rolling_window=rolling_window)
191
192 rolling_beta.plot(alpha=0.7, ax=ax, **kwargs)
~/anaconda/lib/python3.6/site-packages/pyfolio/timeseries.py in rolling_regression(returns, factor_returns, rolling_window)
596 reg = linear_model.LinearRegression(fit_intercept=True).fit(
597 factor_returns.loc[returns_period.index],
--> 598 returns_period)
599 rolling_risk.loc[end, factor_returns.columns] = reg.coef_
600 rolling_risk.loc[end, 'alpha'] = reg.intercept_
~/anaconda/lib/python3.6/site-packages/sklearn/linear_model/base.py in fit(self, X, y, sample_weight)
480 n_jobs_ = self.n_jobs
481 X, y = check_X_y(X, y, accept_sparse=['csr', 'csc', 'coo'],
--> 482 y_numeric=True, multi_output=True)
483
484 if sample_weight is not None and np.atleast_1d(sample_weight).ndim > 1:
~/anaconda/lib/python3.6/site-packages/sklearn/utils/validation.py in check_X_y(X, y, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
571 X = check_array(X, accept_sparse, dtype, order, copy, force_all_finite,
572 ensure_2d, allow_nd, ensure_min_samples,
--> 573 ensure_min_features, warn_on_dtype, estimator)
574 if multi_output:
575 y = check_array(y, 'csr', force_all_finite=True, ensure_2d=False,
~/anaconda/lib/python3.6/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
451 % (array.ndim, estimator_name))
452 if force_all_finite:
--> 453 _assert_all_finite(array)
454
455 shape_repr = _shape_repr(array.shape)
~/anaconda/lib/python3.6/site-packages/sklearn/utils/validation.py in _assert_all_finite(X)
42 and not np.isfinite(X).all()):
43 raise ValueError("Input contains NaN, infinity"
---> 44 " or a value too large for %r." % X.dtype)
45
46
ValueError: Input contains NaN, infinity or a value too large for dtype('float64').
Do you have nans in your returns / factor_returns?
Double checked, no nans or inf in any series. Maybe an issue with the frequency of my data in business days? data=(data.resample('B').last())
The original issue here is separate from the latest issue you guys brought up — the latest issue is related to this: https://github.com/quantopian/pyfolio/issues/462.
I'll put in a fix for this, it's currently blocking me as well.
try np.squeeze(). It may work.
Just FYI, nobody has mentioned; but this problem seems to be caused from a numpy bug, especially in np.delete
. I encountered a quite similar error when using numpy.
And I avoid this error with the code in this link: https://stackoverflow.com/questions/19286657/index-all-except-one-item-in-python?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals.py in create_block_manager_from_blocks(blocks, axes)
4621 blocks = [make_block(values=blocks[0],
-> 4622 placement=slice(0, len(axes[0])))]
4623
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals.py in make_block(values, placement, klass, ndim, dtype, fastpath)
2956
-> 2957 return klass(values, ndim=ndim, fastpath=fastpath, placement=placement)
2958
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals.py in __init__(self, values, placement, ndim, fastpath)
119 'implies %d' % (len(self.values),
--> 120 len(self.mgr_locs)))
121
ValueError: Wrong number of items passed 5, placement implies 1
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-26-261beb8fa7e1> in <module>()
----> 1 ddf=pd.DataFrame(lm.coef_,X.columns,columns=['coeffs'])
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in __init__(self, data, index, columns, dtype, copy)
359 else:
360 mgr = self._init_ndarray(data, index, columns, dtype=dtype,
--> 361 copy=copy)
362 elif isinstance(data, (list, types.GeneratorType)):
363 if isinstance(data, types.GeneratorType):
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in _init_ndarray(self, values, index, columns, dtype, copy)
531 values = maybe_infer_to_datetimelike(values)
532
--> 533 return create_block_manager_from_blocks([values], [columns, index])
534
535 @property
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals.py in create_block_manager_from_blocks(blocks, axes)
4629 blocks = [getattr(b, 'values', b) for b in blocks]
4630 tot_items = sum(b.shape[0] for b in blocks)
-> 4631 construction_error(tot_items, blocks[0].shape[1:], axes, e)
4632
4633
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals.py in construction_error(tot_items, block_shape, axes, e)
4606 raise ValueError("Empty data passed with indices specified.")
4607 raise ValueError("Shape of passed values is {0}, indices imply {1}".format(
-> 4608 passed, implied))
4609
4610
ValueError: Shape of passed values is (5, 1), indices imply (1, 5)
Assignment the dataframe to another variable and run the code
I just installed pyfolio and am trying a quick test, but get an error. I am on ubuntu 16.04 and python3.