Skip to content

ValueError: ['End'] not contained in the index #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
brunocapelao opened this issue Sep 24, 2019 · 6 comments
Closed

ValueError: ['End'] not contained in the index #21

brunocapelao opened this issue Sep 24, 2019 · 6 comments
Labels
bug Something isn't working

Comments

@brunocapelao
Copy link

Hello,

I'm using the lib with a dataset like this:

image

and I have the following error:

C:\Users\capel\Anaconda3\lib\site-packages\backtesting\_util.py:32: FutureWarning: Converting timezone-aware DatetimeArray to timezone-naive ndarray with 'datetime64[ns]' dtype. In the future, this will return an ndarray with 'object' dtype where each element is a 'pandas.Timestamp' with the correct 'tz'.
	To accept the future behavior, pass 'dtype=object'.
	To keep the old behavior, pass 'dtype="datetime64[ns]"'.
  obj = np.asarray(array).view(cls)
C:\Users\capel\Anaconda3\lib\site-packages\backtesting\_util.py:23: FutureWarning: The default of the 'keep_tz' keyword will change to True in a future release. You can set 'keep_tz=True' to obtain the future behaviour and silence this warning.
  return df.index[:100].to_series().diff().median()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-b0362348a33a> in <module>
      1 backtest = Backtest(dados, BarStrategy, commission=.0075, cash=100)
----> 2 backtest.run()

~\Anaconda3\lib\site-packages\backtesting\backtesting.py in run(self, **kwargs)
    703                 strategy.next()
    704 
--> 705         self._results = self._compute_stats(broker, strategy)
    706         return self._results
    707 

~\Anaconda3\lib\site-packages\backtesting\backtesting.py in _compute_stats(self, broker, strategy)
    870         s = pd.Series()
    871         s['Start'] = df.index[0]
--> 872         s['End'] = df.index[-1]
    873         # Assigning Timedeltas needs the key to exist beforehand,
    874         # otherwise the value is interpreted as nanosec *int*. See:

~\Anaconda3\lib\site-packages\pandas\core\series.py in __setitem__(self, key, value)
   1037         # do the setitem
   1038         cacher_needs_updating = self._check_is_chained_assignment_possible()
-> 1039         setitem(key, value)
   1040         if cacher_needs_updating:
   1041             self._maybe_update_cacher()

~\Anaconda3\lib\site-packages\pandas\core\series.py in setitem(key, value)
   1033                     pass
   1034 
-> 1035             self._set_with(key, value)
   1036 
   1037         # do the setitem

~\Anaconda3\lib\site-packages\pandas\core\series.py in _set_with(self, key, value)
   1083                 self._set_values(key.astype(np.bool_), value)
   1084             else:
-> 1085                 self._set_labels(key, value)
   1086 
   1087     def _set_labels(self, key, value):

~\Anaconda3\lib\site-packages\pandas\core\series.py in _set_labels(self, key, value)
   1093         mask = indexer == -1
   1094         if mask.any():
-> 1095             raise ValueError('%s not contained in the index' % str(key[mask]))
   1096         self._set_values(indexer, value)
   1097 

ValueError: ['End'] not contained in the index

I suggest change this code to use the .loc method expressly, like this:

 871         s.loc['Start'] = df.index[0]
 872         s.loc['End'] = df.index[-1]
@kernc
Copy link
Owner

kernc commented Sep 24, 2019

Please update to backtesting 0.1.2 released yesterday. This should work.

pip install -U backtesting

@brunocapelao
Copy link
Author

This does not work either.

image

@kernc
Copy link
Owner

kernc commented Sep 25, 2019

Thanks. Any way I could get ahold of some of your data that reproduces this?

@brunocapelao
Copy link
Author

I tested on Linux and it works.

@kernc
Copy link
Owner

kernc commented Sep 26, 2019

But it still doesn't work on Windos or what? 😕

An explicit .loc sure can't hurt. Care to open a PR for it?

@kernc kernc added the bug Something isn't working label Sep 26, 2019
@marvinbelfort
Copy link

Found same bug. Apparently is some problem with timedelta64 having an UTC info attached.

s['Start'] = df.index[0]
# FIXME: works for df.index having dtype: datetime64[ns] but not for dtype: datetime64[ns, UTC].
s['End'] = df.index[-1]	

if you do remove timezones from your dataframe, it will work:

mydataframe.index = mydataframe.index.tz_localize(None)

It is a hack, but will have you going until a better solution is found.

@kernc kernc closed this as completed in a46d72a Oct 1, 2019
Goblincomet pushed a commit to Goblincomet/forex-trading-backtest that referenced this issue Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants