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'

1 answer

  • answered 2020-06-27 04:20 Aditya

    You should remember this,

    • String to a datetime object, we need to use strptime..
    • A datetime object to other formats, we need to use 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;