简体   繁体   English

IEX Cloud API 出现“ValueError: year is out of range”

[英]"ValueError: year is out of range" with IEX Cloud API

I have a CSV (which I converted to a dataframe) consisting of company/stock data:我有一个由公司/股票数据组成的 CSV(我将其转换为数据框):

  Symbol  Quantity  Price  Cost      date
0    DIS         9    NaN    20  20180531
1   SBUX         5    NaN    30  20180228
2   PLOW         4    NaN    40  20180731
3   SBUX         2    NaN    50  20191130
4    DIS        11    NaN    25  20171031

And I am trying to use the IEX Cloud API to pull in the stock Price for a given date .我正在尝试使用 IEX Cloud API 来获取给定date的股票Price And then ultimately write that to the dataframe.然后最终将其写入数据帧。 Per the IEX Cloud API documentation , I can use the get_historical_data function, where the 2nd argument is the date : df = get_historical_data("SBUX", "20190617", close_only=True)根据IEX Cloud API 文档,我可以使用get_historical_data函数,其中第二个参数是datedf = get_historical_data("SBUX", "20190617", close_only=True)

Everything works fine so long as I pass in a raw date directly to the function (eg, 20190617 ), but if I try using a variable instead, I get ValueError: year 20180531 is out of range .只要我将原始日期直接传递给函数(例如20190617 ),一切都可以正常工作,但是如果我尝试使用变量,我会得到ValueError: year 20180531 is out of range I'm guessing something is wrong with the date format in my original CSV?我猜我的原始 CSV 中的date格式有问题?

Here is my full code:这是我的完整代码:

import os
from iexfinance.stocks import get_historical_data
import pandas as pd

os.environ['IEX_API_VERSION'] = 'iexcloud-sandbox'
os.environ['IEX_TOKEN'] = 'Tsk_5798c0ab124d49639bb1575b322841c4'

input_df = pd.read_csv("all.csv")

for index, row in input_df.iterrows():
    symbol = row['Symbol']
    date = row['date']
    temp_df = get_historical_data(symbol, date, close_only=True, output_format='pandas')
    price = temp_df['close'].values[0]

    print(temp_df)

Note that this is a public token, so it's okay to use请注意,这是一个公共令牌,因此可以使用

When you called get_historical_data("SBUX", "20190617", close_only=True) you passed the date as a string .当您调用get_historical_data("SBUX", "20190617", close_only=True)您将日期作为string传递。

But when you read a DataFrame using read_csv , this column (containing 8-digit strings) is converted to an integer .但是当您使用read_csv读取DataFrame 时,此列(包含 8 位字符串)将转换为integer

This difference can be the source of problem.这种差异可能是问题的根源。

Try 2 things:尝试两件事:

  • convert this column to string, or将此列转换为字符串,或
  • while reading the DataFrame, pass dtype={'date': str} , so that this column will be read as a string.在读取 DataFrame 时,传递 dtype ={'date': str} ,以便将此列作为字符串读取。

You should be fine if you transform your date row into datetime .如果您将date行转换为datetime您应该没问题。

import pandas as pd

df = pd.DataFrame(['20180531'])

pd.to_datetime(df.values[:, 0])
Out[43]: DatetimeIndex(['2018-05-31'], dtype='datetime64[ns]', freq=None)

Then, your column will be correctly formatted for use elsewhere.然后,您的列将被正确格式化以在其他地方使用。 You can insert this line below pd.read_csv() :您可以在pd.read_csv()下方插入此行:

df['date'] = pd.to_datetime(df['date'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM