backtesting.py icon indicating copy to clipboard operation
backtesting.py copied to clipboard

The get_loc() method of the Index class in pandas does not have a method argument.

Open chekh opened this issue 1 year ago • 5 comments
trafficstars

Expected Behavior

Executing Backtest.plot() raise an exception in backtesting._plotting module in function def _group_trades(column).

new_bar_idx = new_index.get_loc(mean_time, method='nearest')
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Index.get_loc() got an unexpected keyword argument 'method'

The get_loc() method of the Index class in pandas does not have a method argument.

Original code which is rising exception:


    def _group_trades(column):
        def f(s, new_index=pd.Index(df.index.view(int)), bars=trades[column]):
            if s.size:
                # Via int64 because on pandas recently broken datetime
                mean_time = int(bars.loc[s.index].view(int).mean())
                new_bar_idx = new_index.get_loc(mean_time, method='nearest')
                return new_bar_idx
        return f

As a solution the following code snippet may be applied:


    def _group_trades(column):
        def f(s, new_index=pd.Index(df.index.view(int)), bars=trades[column]):
            if s.size:
                # Via int64 because on pandas recently broken datetime
                mean_time = int(bars.loc[s.index].view(int).mean())

                new_bar_idx = new_index.searchsorted(mean_time, side='left')

                return new_bar_idx
        return f

Additional info

  • Backtesting version: 0.3.3
  • pandas version: 2.1.3

chekh avatar Dec 26 '23 11:12 chekh

For some reason the version when installed by pip doesn't work, but installing by using "pip install -e <repository_path>" works perfectly.

craquinterogo avatar Jan 07 '24 15:01 craquinterogo

Patch works great thx !

RaccoonTrading avatar Jan 17 '24 18:01 RaccoonTrading

Here is a fork in which the error does not occur. It was solved by locking old versions in the dependencies. The aim was to create a stable version. We will subsequently migrate to newer versions.

https://github.com/LUCIT-Systems-and-Development/lucit-backtesting

oliver-zehentleitner avatar Jun 06 '24 22:06 oliver-zehentleitner

It would be better to update, instead of being locked into an unsupported version of Pandas.

https://pandas.pydata.org/docs/whatsnew/v1.4.0.html#other-deprecations :

Deprecated method argument in Index.get_loc(), use index.get_indexer([label], method=...) instead.

vladiscripts avatar Jun 18 '24 17:06 vladiscripts

Of course you're right!

My goal was to have a working version. I wasn't sure whether further side effects would occur if we updated bokeh in the dependencies. with the old version we were on the safe side.

But we will update the code accordingly, here is the current project list: https://github.com/orgs/LUCIT-Systems-and-Development/projects/2/views/1

oliver-zehentleitner avatar Jun 27 '24 08:06 oliver-zehentleitner