ranaroussi / yfinance

Download market data from Yahoo! Finance's API
https://aroussi.com/post/python-yahoo-finance
Apache License 2.0
12.9k stars 2.29k forks source link

Getting an exception for some tickers when fetching historical data for interval="1mo" and period="50mo" #1550

Closed adityanparikh closed 1 year ago

adityanparikh commented 1 year ago

I'm trying to build a screener and everything works fine for most tickers. But for certain few tickers I get the below exception. I checked the Yahoo finance website and the data is all there for those tickers that are returning the exception. It seems to work fine when I change either the period or interval.

Working Examples

print(yf.Ticker("F").history(period="49mo",interval="1mo")) print(yf.Ticker("F").history(period="51mo",interval="1mo")) print(yf.Ticker("F").history(period="50mo",interval="3mo"))

Returning Exception

print(yf.Ticker("F").history(period="50mo",interval="1mo"))

Exception

DEBUG F: Yahoo GET parameters: {'range': '50mo', 'interval': '1mo', 'includePrePost': True, 'events': 'div,splits,capitalGains'} DEBUG F: yfinance received OHLC data: 2019-05-01 04:00:00 -> 2023-06-07 20:03:08 DEBUG F: OHLC after cleaning: 2019-05-01 00:00:00-04:00 -> 2023-06-01 00:00:00-04:00 Traceback (most recent call last): File "c:\Users\Documents\stock-screener\test.py", line 7, in print(yf.Ticker("F").history(period="50mo",interval="1mo",prepost=True)) File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\yfinance\base.py", line 388, in history df = utils.safe_merge_dfs(df, dividends, interval) File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\yfinance\utils.py", line 704, in safe_merge_dfs raise Exception("Lost data during merge despite all attempts to align data (see above)") Exception: Lost data during merge despite all attempts to align data (see above)

ValueRaider commented 1 year ago

I'll handle this.

ValueRaider commented 1 year ago

Can you try Git branch fix/events-merging ? Instructions in #1080

adityanparikh commented 1 year ago

Thank for the quick turnaround, I was able to get the proper response using this branch.

adityanparikh commented 1 year ago

Just FYI, could be unrelated but started seeing a different exception with a different line of code from this fix/events-merging branch. I went back and checked into the main branch and the said exception is not thrown with the same line of code.

print(yf.Ticker("BAOS").history(period="50wk",interval="1wk"))

Traceback (most recent call last): File "c:\Users\Documents\stock-screener\test.py", line 3, in print(yf.Ticker("BAOS").history(period="50wk",interval="1wk")) File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\yfinance\base.py", line 394, in history df = utils.safe_merge_dfs(df, splits, interval) File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\yfinance\utils.py", line 587, in safe_merge_dfs td = _interval_to_timedelta(interval) File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\yfinance\utils.py", line 356, in _interval_to_timedelta return _pd.Timedelta(days=7, unit='d') File "pandas_libs\tslibs\timedeltas.pyx", line 1350, in pandas._libs.tslibs.timedeltas.Timedelta.new TypeError: Invalid datetime unit in metadata string "[d]"

ValueRaider commented 1 year ago

It is unrelated, and odd only you get that error. What is pandas version?

You might be able to fix by deleting part , unit='d', it's not necessary.

adityanparikh commented 1 year ago

That's odd. But as per your suggestion deleting , unit='d' from \site-packages\yfinance\utils.py seems to have fixed it for me. Thanks again.

ValueRaider commented 1 year ago

Leave issue open until fixed by an official PIP release.

Also feel free to patch that timedelta bug with a pull request.