Skip to content

Commit

Permalink
Merge pull request #1740 from mikez/main
Browse files Browse the repository at this point in the history
Fix pandas FutureWarning: "Passing literal html to 'read_html' is deprecated"
  • Loading branch information
ValueRaider authored Nov 9, 2023
2 parents 308e58b + b696add commit 6d3d6b6
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 72 deletions.
141 changes: 71 additions & 70 deletions tests/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,76 +325,77 @@ def test_actions(self):
self.assertFalse(data.empty, "data is empty")


# Below will fail because not ported to Yahoo API
# class TestTickerEarnings(unittest.TestCase):
# session = None

# @classmethod
# def setUpClass(cls):
# cls.session = session_gbl

# @classmethod
# def tearDownClass(cls):
# if cls.session is not None:
# cls.session.close()

# def setUp(self):
# self.ticker = yf.Ticker("GOOGL", session=self.session)

# def tearDown(self):
# self.ticker = None

# def test_earnings(self):
# data = self.ticker.earnings
# self.assertIsInstance(data, pd.DataFrame, "data has wrong type")
# self.assertFalse(data.empty, "data is empty")

# data_cached = self.ticker.earnings
# self.assertIs(data, data_cached, "data not cached")

# def test_quarterly_earnings(self):
# data = self.ticker.quarterly_earnings
# self.assertIsInstance(data, pd.DataFrame, "data has wrong type")
# self.assertFalse(data.empty, "data is empty")

# data_cached = self.ticker.quarterly_earnings
# self.assertIs(data, data_cached, "data not cached")

# def test_earnings_forecasts(self):
# data = self.ticker.earnings_forecasts
# self.assertIsInstance(data, pd.DataFrame, "data has wrong type")
# self.assertFalse(data.empty, "data is empty")

# data_cached = self.ticker.earnings_forecasts
# self.assertIs(data, data_cached, "data not cached")

# def test_earnings_dates(self):
# data = self.ticker.earnings_dates
# self.assertIsInstance(data, pd.DataFrame, "data has wrong type")
# self.assertFalse(data.empty, "data is empty")

# data_cached = self.ticker.earnings_dates
# self.assertIs(data, data_cached, "data not cached")

# def test_earnings_trend(self):
# data = self.ticker.earnings_trend
# self.assertIsInstance(data, pd.DataFrame, "data has wrong type")
# self.assertFalse(data.empty, "data is empty")

# data_cached = self.ticker.earnings_trend
# self.assertIs(data, data_cached, "data not cached")

# def test_earnings_dates_with_limit(self):
# # use ticker with lots of historic earnings
# ticker = yf.Ticker("IBM")
# limit = 110
# data = ticker.get_earnings_dates(limit=limit)
# self.assertIsInstance(data, pd.DataFrame, "data has wrong type")
# self.assertFalse(data.empty, "data is empty")
# self.assertEqual(len(data), limit, "Wrong number or rows")

# data_cached = ticker.get_earnings_dates(limit=limit)
# self.assertIs(data, data_cached, "data not cached")
class TestTickerEarnings(unittest.TestCase):
session = None

@classmethod
def setUpClass(cls):
cls.session = session_gbl

@classmethod
def tearDownClass(cls):
if cls.session is not None:
cls.session.close()

def setUp(self):
self.ticker = yf.Ticker("GOOGL", session=self.session)

def tearDown(self):
self.ticker = None

def test_earnings_dates(self):
data = self.ticker.earnings_dates
self.assertIsInstance(data, pd.DataFrame, "data has wrong type")
self.assertFalse(data.empty, "data is empty")

def test_earnings_dates_with_limit(self):
# use ticker with lots of historic earnings
ticker = yf.Ticker("IBM")
limit = 110
data = ticker.get_earnings_dates(limit=limit)
self.assertIsInstance(data, pd.DataFrame, "data has wrong type")
self.assertFalse(data.empty, "data is empty")
self.assertEqual(len(data), limit, "Wrong number or rows")

data_cached = ticker.get_earnings_dates(limit=limit)
self.assertIs(data, data_cached, "data not cached")

# Below will fail because not ported to Yahoo API

# def test_earnings(self):
# data = self.ticker.earnings
# self.assertIsInstance(data, pd.DataFrame, "data has wrong type")
# self.assertFalse(data.empty, "data is empty")

# data_cached = self.ticker.earnings
# self.assertIs(data, data_cached, "data not cached")

# def test_quarterly_earnings(self):
# data = self.ticker.quarterly_earnings
# self.assertIsInstance(data, pd.DataFrame, "data has wrong type")
# self.assertFalse(data.empty, "data is empty")

# data_cached = self.ticker.quarterly_earnings
# self.assertIs(data, data_cached, "data not cached")

# def test_earnings_forecasts(self):
# data = self.ticker.earnings_forecasts
# self.assertIsInstance(data, pd.DataFrame, "data has wrong type")
# self.assertFalse(data.empty, "data is empty")

# data_cached = self.ticker.earnings_forecasts
# self.assertIs(data, data_cached, "data not cached")

# data_cached = self.ticker.earnings_dates
# self.assertIs(data, data_cached, "data not cached")

# def test_earnings_trend(self):
# data = self.ticker.earnings_trend
# self.assertIsInstance(data, pd.DataFrame, "data has wrong type")
# self.assertFalse(data.empty, "data is empty")

# data_cached = self.ticker.earnings_trend
# self.assertIs(data, data_cached, "data not cached")


class TestTickerHolders(unittest.TestCase):
Expand Down
3 changes: 2 additions & 1 deletion yfinance/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from __future__ import print_function

import datetime as _datetime
from io import StringIO
import json as _json
import logging
import time as _time
Expand Down Expand Up @@ -2090,7 +2091,7 @@ def get_earnings_dates(self, limit=12, proxy=None) -> Optional[pd.DataFrame]:
"the issue. Thank you for your patience.")

try:
data = pd.read_html(data)[0]
data = pd.read_html(StringIO(data))[0]
except ValueError:
if page_offset == 0:
# Should not fail on first page
Expand Down
4 changes: 3 additions & 1 deletion yfinance/scrapers/holders.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from io import StringIO

import pandas as pd

from yfinance.data import TickerData
Expand Down Expand Up @@ -36,7 +38,7 @@ def _scrape(self, proxy):
ticker_url = f"{self._SCRAPE_URL_}/{self._data.ticker}"
try:
resp = self._data.cache_get(ticker_url + '/holders', proxy=proxy)
holders = pd.read_html(resp.text)
holders = pd.read_html(StringIO(resp.text))
except Exception:
holders = []

Expand Down

0 comments on commit 6d3d6b6

Please sign in to comment.