Skip to content

Pandas Plot Fails to Open w/ Method Error #57468

@christopher-silvey

Description

@christopher-silvey

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
from backtesting import Backtest, Strategy
from backtesting.lib import crossover

# Artificial DataFrame creation
data = {
    'datetime': pd.date_range(start='2021-03-11', periods=7, freq='H'),
    'open': [0.3871, 0.336, 0.3416, 0.34, 0.339, 0.4696, 0.4609],
    'high': [0.3872, 0.349, 0.3576, 0.3478, 0.59, 0.496, 0.476],
    'low': [0.314, 0.3352, 0.34, 0.3376, 0.3386, 0.4056, 0.4074],
    'close': [0.3352, 0.3416, 0.34, 0.339, 0.4667, 0.4609, 0.4243],
    'volume': [38135576.5, 8590583.9, 11334792.4, 4362490.6, 78260624.4, 52249994.4, 33508243.7]
}
df = pd.DataFrame(data).set_index('datetime')

# Minimal Strategy Implementation
class MinimalStrategy(Strategy):
    def init(self):
        pass

    def next(self):
        if not self.position:
            self.buy()

# Backtest Setup
bt = Backtest(df, MinimalStrategy, cash=1_000, commission=.002)

# Run the backtest
output = bt.run()
print(output)

# Plot the results
bt.plot()

Issue Description

Traceback (most recent call last):
File "/Users/hidden/Development/Crypto/Bots/BootCamp/Algo/My_Backtesting/sma_backtest_v1.py", line 60, in
bt.plot()
File "/Users/hidden/anaconda3/envs/BootCamp_AlgoTrading/lib/python3.10/site-packages/backtesting/backtesting.py", line 1592, in plot
return plot(
File "/Users/hidden/anaconda3/envs/BootCamp_AlgoTrading/lib/python3.10/site-packages/backtesting/_plotting.py", line 203, in plot
df, indicators, equity_data, trades = _maybe_resample_data(
File "/Users/hidden/anaconda3/envs/BootCamp_AlgoTrading/lib/python3.10/site-packages/backtesting/_plotting.py", line 153, in _maybe_resample_data
trades = trades.assign(count=1).resample(freq, on='ExitTime', label='right').agg(dict(
File "/Users/hidden/anaconda3/envs/BootCamp_AlgoTrading/lib/python3.10/site-packages/pandas/core/resample.py", line 352, in aggregate
result = ResamplerWindowApply(self, func, args=args, kwargs=kwargs).agg()
File "/Users/hidden/anaconda3/envs/BootCamp_AlgoTrading/lib/python3.10/site-packages/pandas/core/apply.py", line 190, in agg
return self.agg_dict_like()
File "/Users/hidden/anaconda3/envs/BootCamp_AlgoTrading/lib/python3.10/site-packages/pandas/core/apply.py", line 423, in agg_dict_like
return self.agg_or_apply_dict_like(op_name="agg")
File "/Users/hidden/anaconda3/envs/BootCamp_AlgoTrading/lib/python3.10/site-packages/pandas/core/apply.py", line 1608, in agg_or_apply_dict_like
result_index, result_data = self.compute_dict_like(
File "/Users/hidden/anaconda3/envs/BootCamp_AlgoTrading/lib/python3.10/site-packages/pandas/core/apply.py", line 496, in compute_dict_like
results = [
File "/Users/hidden/anaconda3/envs/BootCamp_AlgoTrading/lib/python3.10/site-packages/pandas/core/apply.py", line 497, in
getattr(obj._gotitem(key, ndim=1), op_name)(how, **kwargs)
File "/Users/hidden/anaconda3/envs/BootCamp_AlgoTrading/lib/python3.10/site-packages/pandas/core/groupby/generic.py", line 294, in aggregate
return self._python_agg_general(func, *args, **kwargs)
File "/Users/hidden/anaconda3/envs/BootCamp_AlgoTrading/lib/python3.10/site-packages/pandas/core/groupby/generic.py", line 327, in _python_agg_general
result = self._grouper.agg_series(obj, f)
File "/Users/hidden/anaconda3/envs/BootCamp_AlgoTrading/lib/python3.10/site-packages/pandas/core/groupby/ops.py", line 863, in agg_series
result = self._aggregate_series_pure_python(obj, func)
File "/Users/hidden/anaconda3/envs/BootCamp_AlgoTrading/lib/python3.10/site-packages/pandas/core/groupby/ops.py", line 884, in _aggregate_series_pure_python
res = func(group)
File "/Users/hidden/anaconda3/envs/BootCamp_AlgoTrading/lib/python3.10/site-packages/pandas/core/groupby/generic.py", line 324, in
f = lambda x: func(x, *args, **kwargs)
File "/Users/hidden/anaconda3/envs/BootCamp_AlgoTrading/lib/python3.10/site-packages/backtesting/_plotting.py", line 148, in f
new_bar_idx = new_index.get_loc(mean_time, method='nearest')
TypeError: Index.get_loc() got an unexpected keyword argument 'method'

Expected Behavior

It fails to open the graph in a new window.

Installed Versions

INSTALLED VERSIONS

commit : fd3f571
python : 3.10.13.final.0
python-bits : 64
OS : Darwin
OS-release : 22.4.0
Version : Darwin Kernel Version 22.4.0: Mon Mar 6 20:59:58 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T6020
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.0
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.8.2
setuptools : 69.1.0
pip : 24.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 5.1.0
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.1.3
IPython : None
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 15.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.12.0
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds InfoClarification about behavior needed to assess issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions