pandas icon indicating copy to clipboard operation
pandas copied to clipboard

Pandas Plot Fails to Open w/ Method Error

Open magentoman2009 opened this issue 1 year ago • 4 comments

Pandas version checks

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

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

  • [X] 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 : fd3f57170aa1af588ba877e8e28c158a20a4886d 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

magentoman2009 avatar Feb 17 '24 09:02 magentoman2009

Thanks for the report. If you think this is a pandas bug, can you provide a minimal reproducible example. In particular, remove any dependency on third party packages as much as possible.

rhshadrach avatar Feb 17 '24 11:02 rhshadrach

Also, can you give this issue a meaningful title? Currently it is BUG:.

rhshadrach avatar Feb 17 '24 11:02 rhshadrach

@rhshadrach No problem at all. I have updated the title, sorry for overlooking that. I'll post some meaningful code here later today so you can reproduce the issue.

Thank you!

magentoman2009 avatar Feb 17 '24 16:02 magentoman2009

@rhshadrach,

I figured it out. I'm not sure if it is a bug or not or if it was designed this way. In my csv file, I did not have an index column. I was running this:

data = pd.read_csv('/path/to/my.csv', index_col=0, parse_dates=True)

However, when I tried setting index_col to False and specifying which column to get the parse_dates from the error went away:

data = pd.read_csv('/path/to/my.csv', index_col=False, parse_dates=['datetime'])

Now my graph popped up in a new window. Is this a bug? Couldn't Panda account for this mistake if the index column is missing?

magentoman2009 avatar Feb 17 '24 18:02 magentoman2009

The error you're encountering is due to a compatibility issue between the pandas library and the backtesting library. The backtesting library might not be fully compatible with the version of pandas you are using, leading to the TypeError you mentioned.

arta0 avatar Feb 18 '24 21:02 arta0

Thanks for the details @magentoman2009

Is this a bug? Couldn't Panda account for this mistake if the index column is missing?

No - pandas can not detect what should be an index and what should not be from the CSV format. E.g.

df = pd.DataFrame({"a": [1, 1, 2], "b": [3, 4, 5]})
df.to_csv("test.csv", index=False)

print(pd.read_csv("test.csv"))
#    a  b
# 0  1  3
# 1  1  4
# 2  2  5

print(pd.read_csv("test.csv", index_col=0))
#    b
# a   
# 1  3
# 1  4
# 2  5

Closing.

rhshadrach avatar Feb 19 '24 02:02 rhshadrach

I am having this problem too. Are you saying that the fix is not to have an index on your dataframe?

gminorcoles avatar Jun 23 '24 18:06 gminorcoles