簡體   English   中英

我有一個錯誤提示 ValueError: time data '31 days, 0:00:00' does not match format '%Y-%m-%d'

[英]I have an error that says ValueError: time data '31 days, 0:00:00' does not match format '%Y-%m-%d'

我正在嘗試使用此方法檢索股票數據,但它給了我關於日期格式的錯誤。 我很困惑請幫忙

我的方法

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)

我的錯誤

> 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'

你應該記住這一點,

  • 字符串到datetime時間 object,我們需要使用strptime ..
  • 一個datetime時間 object 到其他格式,我們需要使用strftime ..

嘗試這個,

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM