yfinance: JSONDecodeError while downloading stock data
Hi,
When I use yf.download it raises JSONDecodeError('Expecting value: line 1 column 1 (char 0)') .
But it worked well before (The last use was yesterday)
yfinance version: yfinance version: 0.2.44
Python version: 3.11.3
I do not want to upgrade my yfinance and code how do I deal with this error
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 15
This is happening because yfinance changed its DataFrame structure. Earlier, the data fetched from yf.download(“AAPL”) was a simple flat DataFrame. But now, yfinance sometimes returns a MultiIndex DataFrame, where the first level of columns is “Price” and the second level contains “Open”, “High”, “Low”, etc.
Why am I getting “The truth value of a Series is ambiguous” error in yfinance data?
✅ Why did this work before?
Previously,
yfinanceused to return a simple DataFrame like this:Here,
row["Open"]worked fine because"Open"was a single column name in the DataFrame.❌ What Broke? (New yfinance Format)
Now,
yfinancesometimes returns a MultiIndex DataFrame, like this:Here,
"Open","High", etc., are nested under “Price”, sorow["Open"]returns a Series instead of a single value.This causes the error:
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
✅ Solution: Flatten the MultiIndex
To properly access stock data, add this check before iterating over rows:
Now, stock_data looks like this:
Each column (
"Open","Close", etc.) is directly accessible, so this now works fine:Key Takeaway
If you’re getting this error while processing yfinance data, check if the DataFrame has a MultiIndex. If so, extract the relevant ticker’s data using .xs() to flatten it.
Hope this helps!
Thank you so much. this fixes my issue.
The real solution is read the docs https://ranaroussi.github.io/yfinance/reference/index.html
I had the same problem on version 0.2.37 too. I just updated it to 0.2.54 it working now however the return structure of data frame has been change so you might have to match it to your code my example I change it from
to
hope this might help you get and others that have the same problem get some idea to fix it.
Same issue here, even without upgrading
Name: yfinance Version: 0.2.41
I upgraded to latest release 0.2.54 but now I’m getting the following error
“The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().”