Skip to content
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

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

Open
chekh opened this issue Dec 26, 2023 · 2 comments

Comments

@chekh
Copy link

chekh commented Dec 26, 2023

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
@craquinterogo
Copy link

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

@RaccoonTrading
Copy link

Patch works great thx !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants