pandas 值错误:序数必须 >= 1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45257475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ValueError: ordinal must be >= 1
提问by BlueQuant
This Code, Takes 2 co-ordinates for a straight line from google finance and places 3rd point on the same line at some distance.
此代码采用 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()
the following error occurs with lots of Traceback
大量回溯会发生以下错误
dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC)
ValueError: ordinal must be >= 1
dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC)
ValueError: ordinal must be >= 1
The above code works well when I just plot the numeric values without using datetime and pandas. I think the issue might be in datetime or in matplotlib.
当我只绘制数值而不使用日期时间和Pandas时,上面的代码运行良好。我认为问题可能出在日期时间或 matplotlib 中。
I know this question might look duplicate, but I cannot relate my problem to any other solutions here.
我知道这个问题可能看起来重复,但我无法将我的问题与此处的任何其他解决方案联系起来。
回答by Jughead
The error is due to matplotlib's inability to find the location of x-axis value along the x-axis.
该错误是由于 matplotlib 无法沿 x 轴找到 x 轴值的位置。
The plot from the first two lines has numeric values for x-axis, whereas the third line is trying to plot a datetime
on the same axis. While plotting the third line plt.plot(Dates, Highs)
, matplotlib tries to find the x-axis location for the date and fails with the error.
前两行的图具有 x 轴的数值,而第三行试图datetime
在同一轴上绘制 a 。在绘制第三行时plt.plot(Dates, Highs)
,matplotlib 尝试查找日期的 x 轴位置并失败并显示错误。
回答by BlueQuant
Sorry, Actually the error occured due to the 3rd last line. i deleted the plt.plot()Dates, Highs)
And everything Worked like Charm!
抱歉,实际上错误是由于第三行而发生的。我删除了plt.plot()Dates, Highs)
一切都像魅力一样工作!