pandas
pandas copied to clipboard
Pandas Plot Fails to Open w/ Method Error
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
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
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.
Also, can you give this issue a meaningful title? Currently it is BUG:
.
@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!
@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?
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.
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.
I am having this problem too. Are you saying that the fix is not to have an index on your dataframe?