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

Commits related to this issue

Most upvoted comments

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().”

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, yfinance used to return a simple DataFrame like this:

Date Open High Low Close Volume
2025-02-19 184.65 195.60 179.60 192.25 1720300

Here, row["Open"] worked fine because "Open" was a single column name in the DataFrame.


❌ What Broke? (New yfinance Format)

Now, yfinance sometimes returns a MultiIndex DataFrame, like this:

Date Price Price Price Price Price
Ticker Open High Low Close Volume
2025-02-19 184.65 195.60 179.60 192.25 1720300

Here, "Open", "High", etc., are nested under “Price”, so row["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:

if isinstance(stock_data.columns, pd.MultiIndex):
    stock_data = stock_data.xs(key="AAPL", axis=1, level=1)  # Extract relevant ticker's data

Now, stock_data looks like this:

Date Close High Low Open Volume
2025-02-19 934.25 945.59 869.59 884.65 17,203

Each column ("Open", "Close", etc.) is directly accessible, so this now works fine:

if pd.isna(row['Open']):  # No more ambiguity
    continue  

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!

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().”

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, yfinance used to return a simple DataFrame like this:

Date Open High Low Close Volume 2025-02-19 184.65 195.60 179.60 192.25 1720300 Here, row["Open"] worked fine because "Open" was a single column name in the DataFrame.

❌ What Broke? (New yfinance Format)

Now, yfinance sometimes returns a MultiIndex DataFrame, like this:

Date Price Price Price Price Price Ticker Open High Low Close Volume 2025-02-19 184.65 195.60 179.60 192.25 1720300 Here, "Open", "High", etc., are nested under “Price”, so row["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:

if isinstance(stock_data.columns, pd.MultiIndex): stock_data = stock_data.xs(key=“AAPL”, axis=1, level=1) # Extract relevant ticker’s data Now, stock_data looks like this:

Date Close High Low Open Volume 2025-02-19 934.25 945.59 869.59 884.65 17,203 Each column ("Open", "Close", etc.) is directly accessible, so this now works fine:

if pd.isna(row[‘Open’]): # No more ambiguity continue

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.

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

try:
        stock_data = yf.download(stock_name, start=last_trading_day_user_tz.strftime('%Y-%m-%d'),
                                 end=(last_trading_day_user_tz + timedelta(days=1)).strftime('%Y-%m-%d'))
        if not stock_data.empty:
            return round(stock_data['Close'].iloc[-1])
except Exception as e:
        print(f"An error occurred while fetching the stock price: {e}")
        return None

to

try:
        stock_data = yf.download(stock_name, start=last_trading_day_user_tz.strftime('%Y-%m-%d'),
                                 end=(last_trading_day_user_tz + timedelta(days=1)).strftime('%Y-%m-%d'))
        if not stock_data.empty:
            return round(stock_data['Close'][stock_name].iloc[-1])
except Exception as e:
        print(f"An error occurred while fetching the stock price: {e}")
        return None

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().”