Search code examples
pythonfinancestock

'DataFrame' object has no attribute 'split' problem occured when import stock ticker list from .csv file


I have a problem using yfinance: I have to import 101 tickers from .csv file to download close prices.

This is my code:

import pandas as pd
import yfinance as yf
import datetime

tickers = pd.read_csv('symbols.csv', sep=";", header=None, nrows=1)
stocks = yf.download(tickers, start="2020-03-31",end="2020-04-20")
close = stocks['Close']
print(close)

My .csv file has only one row with all the tickers separated by ";". How can I fix It? Thanks! :)


Solution

  • .download() takes a list or a string, but not a DataFrame.

    I would recommend instead reading the file using the csv module:

    import csv 
    
    with open('symbols.csv','r') as f:
        reader = csv.reader(f,delimiter=';')
        tickers = next(reader) # tickers is now a list
    
    stocks = yf.download(tickers, start="2020-03-31",end="2020-04-20")
    

    Result (using AAPL and GOOGL):

    In [19]: stocks
    Out[19]: 
                 Adj Close                    Close  ...         Open    Volume         
                      AAPL        GOOGL        AAPL  ...        GOOGL      AAPL    GOOGL
    Date                                             ...                                
    2020-03-31  254.289993  1161.949951  254.289993  ...  1148.729980  49250500  3261400
    2020-04-01  240.910004  1102.099976  240.910004  ...  1124.000000  44054600  2598500
    2020-04-02  244.929993  1117.030029  244.929993  ...  1100.000000  41483500  2820500
    2020-04-03  241.410004  1092.699951  241.410004  ...  1114.709961  32470000  2568700
    2020-04-06  262.470001  1183.189941  262.470001  ...  1133.000000  50455100  3166000
    2020-04-07  259.429993  1182.560059  259.429993  ...  1217.010010  50721800  3081000
    2020-04-08  266.070007  1207.000000  266.070007  ...  1203.099976  42223800  2016700
    2020-04-09  267.989990  1206.569946  267.989990  ...  1218.180054  40529100  2701400
    2020-04-13  273.250000  1210.410034  273.250000  ...  1201.500000  32755700  1935100
    2020-04-14  287.049988  1265.229980  287.049988  ...  1239.969971  48748700  3167900
    2020-04-15  284.429993  1257.300049  284.429993  ...  1246.510010  32788600  2111800
    2020-04-16  286.690002  1257.430054  286.690002  ...  1267.140015  39281300  2894800
    2020-04-17  282.799988  1279.000000  282.799988  ...  1281.699951  53812500  2552500
    
    [13 rows x 12 columns]