stefan-jansen / machine-learning-for-trading

Code for Machine Learning for Algorithmic Trading, 2nd edition.
https://ml4trading.io
12.57k stars 4.03k forks source link

Daily historical return deciles #302

Open ishikabansal77 opened 1 year ago

ishikabansal77 commented 1 year ago

by_sym = prices.groupby(level='symbol').close for t in T: prices[f'r{t:02}'] = by_sym.pct_change(t) by_sym = prices.groupby(level='symbol').close for t in T: prices[f'r{t:02}'] = by_sym.pct_change(t) for t in T: prices[f'r{t:02}dec'] = (prices[f'r{t:02}'] .groupby(level='date') .apply(lambda x: pd.qcut(x, q=10, labels=False, duplicates='drop')))

IndexError Traceback (most recent call last) Cell In[45], line 2 1 for t in T: ----> 2 prices[f'r{t:02}dec'] = (prices[f'r{t:02}'] 3 .groupby(level='date') 4 .apply(lambda x: pd.qcut(x, 5 q=10, 6 labels=False, 7 duplicates='drop')))

File ~\anaconda3\envs\baclass\lib\site-packages\pandas\core\groupby\generic.py:216, in SeriesGroupBy.apply(self, func, *args, kwargs) 210 @Appender( 211 _apply_docs["template"].format( 212 input="series", examples=_apply_docs["series_examples"] 213 ) 214 ) 215 def apply(self, func, *args, *kwargs) -> Series: --> 216 return super().apply(func, args, kwargs)

File ~\anaconda3\envs\baclass\lib\site-packages\pandas\core\groupby\groupby.py:1353, in GroupBy.apply(self, func, *args, *kwargs) 1351 with option_context("mode.chained_assignment", None): 1352 try: -> 1353 result = self._python_apply_general(f, self._selected_obj) 1354 except TypeError: 1355 # gh-20949 1356 # try again, with .apply acting as a filtering (...) 1360 # fails on some* columns, e.g. a numeric operation 1361 # on a string grouper column 1363 return self._python_apply_general(f, self._obj_with_exclusions)

File ~\anaconda3\envs\baclass\lib\site-packages\pandas\core\groupby\groupby.py:1402, in GroupBy._python_apply_general(self, f, data, not_indexed_same, is_transform, is_agg) 1367 @final 1368 def _python_apply_general( 1369 self, (...) 1374 is_agg: bool = False, 1375 ) -> NDFrameT: 1376 """ 1377 Apply function f in python space 1378 (...) 1400 data after applying f 1401 """ -> 1402 values, mutated = self.grouper.apply(f, data, self.axis) 1403 if not_indexed_same is None: 1404 not_indexed_same = mutated

File ~\anaconda3\envs\baclass\lib\site-packages\pandas\core\groupby\ops.py:767, in BaseGrouper.apply(self, f, data, axis) 765 # group might be modified 766 group_axes = group.axes --> 767 res = f(group) 768 if not mutated and not _is_indexed_like(res, group_axes, axis): 769 mutated = True

Cell In[45], line 4, in (x) 1 for t in T: 2 prices[f'r{t:02}dec'] = (prices[f'r{t:02}'] 3 .groupby(level='date') ----> 4 .apply(lambda x: pd.qcut(x, 5 q=10, 6 labels=False, 7 duplicates='drop')))

File ~\anaconda3\envs\baclass\lib\site-packages\pandas\core\reshape\tile.py:377, in qcut(x, q, labels, retbins, precision, duplicates) 375 x_np = np.asarray(x) 376 x_np = x_np[~np.isnan(x_np)] --> 377 bins = np.quantile(x_np, quantiles) 379 fac, bins = _bins_to_cuts( 380 x, 381 bins, (...) 386 duplicates=duplicates, 387 ) 389 return _postprocess_for_cut(fac, bins, retbins, dtype, original)

File <__array_function__ internals>:180, in quantile(*args, **kwargs)

File ~\anaconda3\envs\baclass\lib\site-packages\numpy\lib\function_base.py:4371, in quantile(a, q, axis, out, overwrite_input, method, keepdims, interpolation) 4369 if not _quantile_is_valid(q): 4370 raise ValueError("Quantiles must be in the range [0, 1]") -> 4371 return _quantile_unchecked( 4372 a, q, axis, out, overwrite_input, method, keepdims)

File ~\anaconda3\envs\baclass\lib\site-packages\numpy\lib\function_base.py:4383, in _quantile_unchecked(a, q, axis, out, overwrite_input, method, keepdims) 4375 def _quantile_unchecked(a, 4376 q, 4377 axis=None, (...) 4380 method="linear", 4381 keepdims=False): 4382 """Assumes that q is in [0, 1], and is an ndarray""" -> 4383 r, k = _ureduce(a, 4384 func=_quantile_ureduce_func, 4385 q=q, 4386 axis=axis, 4387 out=out, 4388 overwrite_input=overwrite_input, 4389 method=method) 4390 if keepdims: 4391 return r.reshape(q.shape + k)

File ~\anaconda3\envs\baclass\lib\site-packages\numpy\lib\function_base.py:3702, in _ureduce(a, func, *kwargs) 3699 else: 3700 keepdim = (1,) a.ndim -> 3702 r = func(a, **kwargs) 3703 return r, keepdim

File ~\anaconda3\envs\baclass\lib\site-packages\numpy\lib\function_base.py:4552, in _quantile_ureduce_func(a, q, axis, out, overwrite_input, method) 4550 else: 4551 arr = a.copy() -> 4552 result = _quantile(arr, 4553 quantiles=q, 4554 axis=axis, 4555 method=method, 4556 out=out) 4557 return result

File ~\anaconda3\envs\baclass\lib\site-packages\numpy\lib\function_base.py:4658, in _quantile(arr, quantiles, axis, method, out) 4650 arr.partition( 4651 np.unique(np.concatenate(([0, -1], 4652 previous_indexes.ravel(), 4653 next_indexes.ravel(), 4654 ))), 4655 axis=DATA_AXIS) 4656 if np.issubdtype(arr.dtype, np.inexact): 4657 slices_having_nans = np.isnan( -> 4658 take(arr, indices=-1, axis=DATA_AXIS) 4659 ) 4660 else: 4661 slices_having_nans = None

File <__array_function__ internals>:180, in take(*args, **kwargs)

File ~\anaconda3\envs\baclass\lib\site-packages\numpy\core\fromnumeric.py:190, in take(a, indices, axis, out, mode) 93 @array_function_dispatch(_take_dispatcher) 94 def take(a, indices, axis=None, out=None, mode='raise'): 95 """ 96 Take elements from an array along an axis. 97 (...) 188 [5, 7]]) 189 """ --> 190 return _wrapfunc(a, 'take', indices, axis=axis, out=out, mode=mode)

File ~\anaconda3\envs\baclass\lib\site-packages\numpy\core\fromnumeric.py:57, in _wrapfunc(obj, method, *args, kwds) 54 return _wrapit(obj, method, *args, *kwds) 56 try: ---> 57 return bound(args, kwds) 58 except TypeError: 59 # A TypeError occurs if the object does have such a method in its 60 # class, but its signature is not identical to that of NumPy's. This (...) 64 # Call _wrapit from within the except clause to ensure a potential 65 # exception has a traceback chain. 66 return _wrapit(obj, method, *args, **kwds)

IndexError: cannot do a non-empty take from an empty axes.

  1. See error

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

Environment If you are not using the latest version of the Docker imag:

Additional context Add any other context about the problem here.

ishikabansal77 commented 1 year ago

Please help