ValueError: to assemble mappings requires at least that [year, month, day] be specified: [day,month,year] is missing

ValueError: to assemble mappings requires at least that [year, month, day] be specified: [day,month,year] is missing

我正在尝试使用 kmeans 进行绘图,但我卡住了,因为其中一列包含日期,这会产生很多问题。 (你可以在截图中看到数据enter image description here)

我已经用过to_datetime,那我现在该怎么办?

这道题怎么过关和出图?

提前致谢!

from sklearn.cluster import KMeans


AAPL= pd.read_csv('AAPL.csv', header=0, squeeze=True)
#sd=store_data.head(100)

x = pd.to_datetime(AAPL.iloc[:, [0,1]],dayfirst=True)
print(x)
kmeans4 = KMeans(n_clusters=4)
y_kmeans4 = kmeans4.fit_predict(x)
print(y_kmeans4)
print(kmeans4.cluster_centers_)

plt.scatter(x[:,0],x[:,1],c=y_kmeans4,cmap='rainbow')
plt.scatter(kmeans4.cluster_centers_[:,0] ,kmeans4.cluster_centers_[:,1],color='black')

您只需要 select 第一列:

x = pd.to_datetime(AAPL.iloc[:, 0],dayfirst=True)

如果使用:

x = pd.to_datetime(AAPL.iloc[:, [0,1]],dayfirst=True)

它 select 第一列和第二列并引发错误,因为 pd.to_datetime 仅在传递列 year, month, days 时才有效,例如 this solution.