ValueError:序数必须 >= 1

Blu*_*ant 5 python matplotlib quantitative-finance dataframe pandas

这段代码,从谷歌金融获取直线的 2 个坐标,并将第三个点放置在同一条线上一定距离处。

 import datetime as dt
 from datetime import timedelta as td
 import matplotlib.pyplot as plt
 from matplotlib import style
 import pandas as pd
 import pandas_datareader.data as web
 import numpy as np

 start = dt.datetime(2017, 7, 1)
 end = dt.datetime(2017, 3, 1)

 # retrieving data from google
 df = web.DataReader('TSLA', 'google', start, )

 Dates = df.index
 Highs = df['High'] # Getting only the values from the 'High' Column.

 Highest_high = np.amax(Highs)  # returns the Highest value
      for i, h in enumerate(Highs):
           if h == Highest_high :
              Highests_index = i
 #Highests_index = Highs.argmax()  # returns the index of Highest value

 Highest_high_2 = sorted(Highs)[-2]
 for i, j in enumerate(Highs):
      if j == Highest_high_2 :
         Highests_index_2 = i

 #================Problem Maybe starting from here========================

 x = [Highests_index, Highests_index_2]
 y = [Highest_high, Highest_high_2]
 coefficients = np.polyfit(x, y, 1)

 polynomial = np.poly1d(coefficients)
 # the np.linspace lets you set number of data points, line length.
 x_axis = np.linspace(3,Highests_index_2 + 1, 3)
 y_axis = polynomial(x_axis)

 plt.plot(x_axis, y_axis)
 plt.plot(x[0], y[0], 'go')
 plt.plot(x[1], y[1], 'go')
 plt.plot(Dates, Highs)
 plt.grid('on')
 plt.show()
Run Code Online (Sandbox Code Playgroud)

大量回溯时会发生以下错误

dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC)
ValueError: 序数必须 >= 1

当我只绘制数值而不使用日期时间和 pandas 时,上面的代码效果很好。我认为问题可能出在日期时间或 matplotlib 中。

我知道这个问题可能看起来重复,但我无法将我的问题与这里的任何其他解决方案联系起来。

Blu*_*ant -1

抱歉,实际上错误是由于最后第三行而发生的。我删除了plt.plot()Dates, Highs)一切都像魅力一样有效!