time data 'yahoo' does not match format '%Y-%m-%d in pandas datareader, python

437 Views Asked by At

I'm trying to get some data in yahoo and using pandas datareader, but I got this error:

['MSFT']: ValueError("time data 'yahoo' does not match format '%Y-%m-%d'")

I'm new to Python.

code

import pandas_datareader as data
import yfinance as yf
import datetime

yf.pdr_override()
start= datetime.datetime(2020, 1, 1)
end =datetime.datetime(2023, 1, 1)

df = data.DataReader('MSFT', 'yahoo', start, end)

print(df)
2

There are 2 best solutions below

0
ApaxPhoenix On

It seems you are trying to use pandas_datareader as data, but I would recommend you using yfinfance directly, by using the method download. This function directly fetches the historical stock data for Microsoft (MSFT) from Yahoo Finance. It should work more reliably for your purpose.

import yfinance as yf
import datetime

start = datetime.datetime(2020, 1, 1)
end = datetime.datetime(2023, 1, 1)

# Fetch data using yfinance
msft = yf.download('MSFT', start=start, end=end)

print(msft)
1
Merri Lambino On

Try this

import yfinance as yf
import datetime

start= datetime.datetime(2020, 1, 1)
end =datetime.datetime(2023, 1, 1)

ticker = yf.Ticker("MSFT")
df = ticker.history(period="max", start=start, end=end)
print(df)