[英]I have an error that says ValueError: time data '31 days, 0:00:00' does not match format '%Y-%m-%d'
I am trying to retrieve stock data using this method but it gives me an error about the date format.我正在尝试使用此方法检索股票数据,但它给了我关于日期格式的错误。 I'm confused please help
我很困惑请帮忙
My Method我的方法
def getPrice(stock):
today = date.today()
monthAgo = today = timedelta(days=31)
data = pdr.get_data_yahoo(stock, start=monthAgo, end=today)
df = pd.DataFrame(data)
for index, row in df.iterrows():
addPrice(row, index.strftime("%Y-%M-%d"), stock)
My Error我的错误
> Traceback (most recent call last):
File "/Users/kutloano/anaconda3/lib/python3.7/threading.py", line 917, in
_bootstrap_inner
self.run()
File "/Users/kutloano/anaconda3/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "/Users/kutloano/anaconda3/lib/python3.7/site- packages/multitasking/__init__.py", line 102, in _run_via_pool
return callee(*args, **kwargs)
File "/Users/kutloano/anaconda3/lib/python3.7/site-packages/yfinance/multi.py",
line 167, in _download_one_threaded
actions, period, interval, prepost, proxy, rounding)
File "/Users/kutloano/anaconda3/lib/python3.7/site-packages/yfinance/multi.py",
line 182, in _download_one
rounding=rounding, many=True)
File "/Users/kutloano/anaconda3/lib/python3.7/site-packages/yfinance/base.py",
line 121, in history
_time.strptime(str(start), '%Y-%m-%d')))
File "/Users/kutloano/anaconda3/lib/python3.7/_strptime.py", line 571, in
_strptime_time
tt = _strptime(data_string, format)[0]
File "/Users/kutloano/anaconda3/lib/python3.7/_strptime.py", line 359, in
_strptime
(data_string, format))
ValueError: time data '31 days, 0:00:00' does not match format '%Y-%m-%d'
You should remember this,你应该记住这一点,
datetime
object, we need to use strptime
..datetime
时间 object,我们需要使用strptime
..datetime
object to other formats, we need to use strftime
..datetime
时间 object 到其他格式,我们需要使用strftime
.. Try this,尝试这个,
from datetime import datetime
datetime.strptime('31 days, 0:00:00', "%d days, %H:%M:%S")
Out[4]: datetime.datetime(1900, 1, 31, 0, 0)
# the year and month are defaults as you don't have them and a date without a year and month doesn't makes sense either;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.