TypeError: ufunc subtract cannot use operands with types dtype('<M8[ns]') and dtype('float64')

TypeError: ufunc subtract cannot use operands with types dtype('<M8[ns]') and dtype('float64')

我按照 senddex 的视频教程遇到了错误代码。但是,当我想在 ax2 上为我的卷绘制条形图时,它会给我主题中列出的错误代码。请帮忙。我是新手 Python 0 编程经验。

import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import pandas_datareader.data as web
style.use('ggplot')

df = pd.read_csv('C:\Users\ngjun95\Downloads\7120.KL.csv',     parse_dates=True, index_col=0)
df['100ma'] = df['Adj Close'].rolling(window=100, min_periods=0).mean()

print(df.head())

ax1 = plt.subplot2grid((6,1), (0,0), rowspan=5, colspan=1)
ax2 = plt.subplot2grid((6,1), (5,0), rowspan=1, colspan=1, sharex=ax1)

ax1.plot(df.index, df['Adj Close'])
ax1.plot(df.index, df['100ma'])
ax2.bar(df.index, df['Volume'])

plt.show()

似乎是 Matplotlib 和 Numpy 之间的日期转换问题。 https://github.com/matplotlib/matplotlib/issues/9610

我遇到同样问题的时间最长。

df.index.to_pydatetime() 适合我。

老话题,但是 df.index.to_pydatetime() 没有解决我使用 seaborn.regplot() 的问题。对我来说,date2num 工作得很好。

import matplotlib.dates as mdates

sns.regplot(x=mdates.date2num(df.index), y=df['target'])