I have stock data that I wanted to play with, and i figured I'd plot it with mpl. This is what I tried:
daily = pd.read_csv('data/AAPL/history.csv',index_col=0,parse_dates=True)
mpf.plot(daily)
This returns
TypeError: Expect data.index as DatetimeIndex
Then I tried this, following the docs
daily = pd.read_csv('data/AAPL/history.csv',index_col=0,parse_dates=True)
daily.index.name = 'Date'
mpf.plot(daily)
This gives the same error.
Then I tried setting the index:
daily = pd.read_csv('data/AAPL/history.csv',index_col=0,parse_dates=True)
daily.index = pd.DatetimeIndex(daily['Date'])
mpf.plot(daily)
With the error:
KeyError: 'Date'
Trying to do the same but without setting the index:
daily = pd.read_csv('data/AAPL/history.csv')
daily.index = pd.DatetimeIndex(daily['Date'])
mpf.plot(daily)
Returns:
TypeError: [datetime.datetime(1980, 12, 12, 0, 0, tzinfo=tzoffset(None, -18000))
datetime.datetime(1980, 12, 15, 0, 0, tzinfo=tzoffset(None, -18000))
datetime.datetime(1980, 12, 16, 0, 0, tzinfo=tzoffset(None, -18000)) ...
datetime.datetime(2023, 11, 10, 0, 0, tzinfo=tzoffset(None, -18000))
datetime.datetime(2023, 11, 13, 0, 0, tzinfo=tzoffset(None, -18000))
datetime.datetime(2023, 11, 14, 0, 0, tzinfo=tzoffset(None, -18000))]
This leads me to believe that one of the rows is not formatted correctly. I got this data off yfinance and I'm just reading it in like you'd expect. But since the number of rows is so large (10,823), I'm a little confused on how to clean the dates, on how to find the bad row, if there is any.
Any help would be appreciated. I don't know if it's my code or if it's my data. I'm led to believe it's my data, but this is my first time messing with this stuff so I don't know.
'Datetime'
column after loading the csv.df = pd.read_csv('aapl.csv', index_col='Datetime')
df.index = pd.to_datetime(df.index, utc=True)
Timestamp('2023-01-03 14:30:00+0000', tz='UTC')
is the resulting format, which does work withmpf.plot(df)
Timestamp('2023-01-03 09:30:00-0500', tz='America/New_York')
, which works withmpf.plot(df)
.df = pd.read_csv('aapl.csv', index_col='Datetime', parse_dates=['Datetime'])
results in:Timestamp('2023-01-03 09:30:00-0500', tz='UTC-05:00')
, which does not work withmpf.plot(df)
.mpf.plot(df)
seems to be particular about the timezone,tz
, format.python v3.12.0
,pandas v2.1.2
,mplfinance v0.12.9b7
,yfinance v0.2.31
,matplotlib v3.8.1
.Load Data from
yfinace
and Plot - WorksTimestamp('2023-01-03 09:30:00-0500', tz='America/New_York')
Load csv and Parse Dates - Plotting Doesn't Work
Timestamp('2023-01-03 09:30:00-0500', tz='UTC-05:00')
TypeError: Expect data.index as DatetimeIndex
Load csv then Parse Dates with
utc=True
and Plot - WorksTimestamp('2023-01-03 14:30:00+0000', tz='UTC')
mpf.plot()
sets a particularstyle
withmatplotlib rcParams
, which doesn't revert until the notebook environment has changed.pandas.DataFrame.plot
, which usesmatplotlib
as the default backend.mplfinance
features can't be used.mpf.plot
pandas.DataFrame.plot
format