Python Forum
Object attribute behavior different in 2 scripts
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Object attribute behavior different in 2 scripts
#1
Hi, in scripts 1 and 2 below I got different behavior on object yf.Ticker.

Script 1 runs fine and in fact so too did a similar Script 2 for a moderator from this forum. But on my PC, Script 2 generates the attribute error you see in item 3. I did learn that I can just set ticker to line.strip("\n\r") but I think I want to understand the attribute error anyway.

import yfinance as yf
import sys

for line in open('C:\\Users\\stant\\OneDrive\\Documents\\daxandpython\\tickers.txt'):
    tkr=yf.Ticker(line.strip("\n\r"))
    with open('C:\\Users\\stant\\OneDrive\\Documents\\daxandpython\\yfinance\\'+line.strip("\n\r") +'.txt', 'w') as sys.stdout:
         tkr.history(period="max")
import yfinance as yf
import pandas as pd
#import sys
from datetime import date
today = date.today()
data = None
for line in open('C:\\Users\\stant\\OneDrive\\Documents\\daxandpython\\tickers.txt'):
    ticker=yf.Ticker(line.strip("\n\r"))
    x = yf.download(ticker, start="2017-01-01", end=today, progress=False).round(2)
    #x["Stock"]=ticker
    x.insert(0, "Ticker", ticker)
    if data is None:
        data = x
    else:
        data = pd.concat((data, x))
    data = data.sort_index()
Error:
Traceback (most recent call last): File "<stdin>", line 3, in <module> File "C:\Users\stant\AppData\Local\Programs\Python\Python311\Lib\site-packages\yfinance\multi.py", line 107, in download tickers, (list, set, tuple)) else tickers.replace(',', ' ').split() ^^^^^^^^^^^^^^^ AttributeError: 'Ticker' object has no attribute 'replace'
Reply
#2
In the first example you don't use tkr (a yf.Ticker object). In the second you pass a yf.Ticker object to yf.download(). yf.download() expects tickers to be strings, not yf.Ticker objects.

I looked at your other threads. This is the first time yf.Ticker makes an appearance. This is all on you. It is not a fault of the modules or the GUI you are using. This is a sloppy programming, lack of attention to detail error. We've all had those and had to learn to have "fresh eyes" when reading code. You need to read the actual code instead of the code you expect to see. You should have seen that yf.Ticker does not appear in the moderator's script and that would make you research what yf.Ticker is, and what arguments are expected by yf.download. From that you would know that yf.Ticker is not the correct type for ticker arguments passed to yf.download.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,755 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,924 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,399 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,684 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  WebDriver' object has no attribute 'find_element_by_css_selector rickadams 3 5,945 Sep-19-2022, 06:11 PM
Last Post: Larz60+
  'dict_items' object has no attribute 'sort' Calli 6 4,541 Jul-29-2022, 09:19 PM
Last Post: Gribouillis
  AttributeError: 'numpy.ndarray' object has no attribute 'load' hobbyist 8 7,143 Jul-06-2022, 10:55 AM
Last Post: deanhystad
  AttributeError: 'numpy.int32' object has no attribute 'split' rf_kartal 6 4,432 Jun-24-2022, 08:37 AM
Last Post: Anushka00
  AttributeError: 'list' object has no attribute 'upper' Anldra12 4 4,913 Apr-27-2022, 09:27 AM
Last Post: Anldra12
  AttributeError: 'function' object has no attribute 'metadata 3lnyn0 5 4,648 Mar-28-2022, 04:42 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020