Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed error: if zone.upper() == 'UTC': AttributeError: 'float' object has no attribute 'upper' #1137

Closed
Slepetys opened this issue Oct 31, 2022 · 0 comments

Comments

@Slepetys
Copy link

Discussed in https://github.com/ranaroussi/yfinance/discussions/1134

Originally posted by Slepetys October 31, 2022
Hi yfinancers,

I would like to report an error and how I fixed it.

In the long cascade of error messages, I found the most informative as:
if zone.upper() == 'UTC': AttributeError: 'float' object has no attribute 'upper'

which is caused when a ticker symbol is passed to yfinance, but it does not exist. In most cases, yfinance handles such kind of situations well, but occasionally it returns an error like the message above.

After digging, I found that the exception handler in the base.py module, history function was not dealing well when the timezone is not defined (either Nan or None) in the prior call.

In version 0.1.84 I found such an error, I fixed just adding an additional exceptional handling step specific to such kind of condition:

First line: 153

            try:
                tz = self._get_ticker_tz(debug_mode, proxy, timeout)

                if tz != tz or tz is None:
                    return utils.empty_df()

            except KeyError as e:
            ...

It is possible to emulate the error through:

# Emulate yFinance error
from datetime import datetime as datetime

import yfinance as yf
print ('yfinance version = '+yf.__version__)
_start = datetime (2022,10,1)
_end   = datetime (2022,10,30)

# Ticker Symbol that exists - NO ERROR:
ts_real = 'AAPL'
_OHLC = yf.download (ts_real,start=_start,end=_end,interval='1d')
print (_OHLC) #No error

# Ticker Symbol NOT exists - NO ERROR!
ts_real = 'AACQ'
_OHLC = yf.download (ts_real,start=_start,end=_end,interval='1d')

And the error follows:

Exception in thread Thread-6:
Traceback (most recent call last):
  File "C:\Users\Roberto\anaconda3\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\Roberto\anaconda3\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Roberto\anaconda3\lib\site-packages\multitasking\__init__.py", line 102, in _run_via_pool
    return callee(*args, **kwargs)
  File "C:\Users\Roberto\anaconda3\lib\site-packages\yfinance\multi.py", line 199, in _download_one_threaded
    data = _download_one(ticker, start, end, auto_adjust, back_adjust,
  File "C:\Users\Roberto\anaconda3\lib\site-packages\yfinance\multi.py", line 213, in _download_one
    return Ticker(ticker).history(period=period, interval=interval,
  File "C:\Users\Roberto\anaconda3\lib\site-packages\yfinance\base.py", line 171, in history
    end = utils._parse_user_dt(end, tz)
  File "C:\Users\Roberto\anaconda3\lib\site-packages\yfinance\utils.py", line 157, in _parse_user_dt
    dt = _tz.timezone(exchange_tz).localize(dt)
  File "C:\Users\Roberto\anaconda3\lib\site-packages\pytz\__init__.py", line 170, in timezone
    if zone.upper() == 'UTC':
AttributeError: 'float' object has no attribute 'upper'

I hope this helps future versions.

All the best
Roberto Slepetys

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants