From 342499a9a9a63e57e3c2d9e0cbcacc46fdc6e225 Mon Sep 17 00:00:00 2001 From: ValueRaider Date: Sun, 28 Apr 2024 19:40:55 +0100 Subject: [PATCH] Fix: datetime.datetime.utcnow() is deprecated ... DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC). --- tests/prices.py | 4 ++-- yfinance/scrapers/history.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/prices.py b/tests/prices.py index 61f633249..ad05da2f3 100644 --- a/tests/prices.py +++ b/tests/prices.py @@ -62,7 +62,7 @@ def test_duplicatingHourly(self): dat = yf.Ticker(tkr, session=self.session) tz = dat._get_ticker_tz(proxy=None, timeout=None) - dt_utc = _tz.timezone("UTC").localize(_dt.datetime.utcnow()) + dt_utc = _dt.datetime.now(_dt.UTC) dt = dt_utc.astimezone(_tz.timezone(tz)) start_d = dt.date() - _dt.timedelta(days=7) df = dat.history(start=start_d, interval="1h") @@ -82,7 +82,7 @@ def test_duplicatingDaily(self): dat = yf.Ticker(tkr, session=self.session) tz = dat._get_ticker_tz(proxy=None, timeout=None) - dt_utc = _tz.timezone("UTC").localize(_dt.datetime.utcnow()) + dt_utc = _dt.datetime.now(_dt.UTC) dt = dt_utc.astimezone(_tz.timezone(tz)) if dt.time() < _dt.time(17, 0): continue diff --git a/yfinance/scrapers/history.py b/yfinance/scrapers/history.py index a94b524a5..6a6b3ad06 100644 --- a/yfinance/scrapers/history.py +++ b/yfinance/scrapers/history.py @@ -210,7 +210,7 @@ def history(self, period="1mo", interval="1d", quotes = utils.parse_quotes(data["chart"]["result"][0]) # Yahoo bug fix - it often appends latest price even if after end date if end and not quotes.empty: - endDt = pd.to_datetime(_datetime.datetime.utcfromtimestamp(end)) + endDt = pd.to_datetime(_datetime.datetime.fromtimestamp(end, _datetime.UTC)).tz_localize(None) if quotes.index[quotes.shape[0] - 1] >= endDt: quotes = quotes.iloc[0:quotes.shape[0] - 1] except Exception: