0

So I have made a class that takes in a ticker symbol, and it returns a dataframe with all the price information for the dates specified. here is the code below:

import pandas as pd
import numpy as np
import pandas_datareader as pdr

# class to get stock price

class GetStockInfo():

    '''
    Class to retrieve stock info and returns it as a dataframe
    '''

    def __init__(self, ticker):

        self.ticker = ticker.upper()

    def build_df(self, start_date, end_date):

        df = pd.DataFrame(pdr.DataReader(self.ticker, 'yahoo', start_date, end_date))

        return df

now this works perfectly, but ideally id like to pass in a list of symbols and have it return a seperate df for each symbol. so for example,

symbols = ['aapl','googl','msft','tsla']

and id like it to return 4 seperate dfs, each named aapl_df, msft_df etc. is there any way to do this?

ive tried using a for loop like so

for i in symbols:
    stock = GetStockInfo(i)

    i_df = stock.build_df('2019-01-01', '2020-01-01')

but im not sure how to get it to return seperate dfs.

3
  • Ok so don't think of this as a how to return multiple dfs. Think of it as, how would you go about returning multiple variables? What needs to be done on the receiving end of the return? Commented May 6, 2020 at 16:41
  • so the build_df function should be coded to take a list, and have some sort of for loop to iterate through the list and return a df for every item passed in? Commented May 6, 2020 at 16:50
  • Should all your symbols have the same start and end date? Then you can probably create another class function like build_multiple_dfs and that basically can do the for loop and list/dict like shown in the answer. And then you just call this particular function! Commented May 6, 2020 at 17:05

2 Answers 2

1

As I commented, you can also do something like this:

Edit:

I guess you don't need to __init__ at all

class GetStockInfo():

    '''
    Class to retrieve stock info and returns it as a dataframe
    '''

    def __init__(self):

        pass

    def build_df(self, stock, start_date, end_date):

        df = pd.DataFrame(pdr.DataReader(stock.upper(), 'yahoo', start_date, end_date))

        return df

    def build_multiple(self,symbols,start_dates,end_dates):
        result = {}
        for i in range(len(symbols)):
            result[symbols[i]] = self.build_df(symbols[i],start_dates[i],end_dates[i])
        return result

Also, I think your question more about how to access those dfs like you mentioned aapl_df,msft_df...?

You can just do results['aapl'] instead of writing aapl_df - and so on.

Sign up to request clarification or add additional context in comments.

8 Comments

this makes sense, but then wouldn't there need to be a list called symbols somewhere in the construction of the class?
Oh! ticker was your stock symbol, no? You can just pass it through the function - because would it not be different for each stock?
yes. so the way the original code I posted works is if for example I do the following: df = GetStockInfo('aapl').build_df('2019-01-01', '2020-01-01') then I would get a df for aapl with the price. but if I want to pass in a list of tickers to get a df for each ticker in that list, thats where I am lost. I get what both of the answers here are trying to do, but I dont see how to implement that in my code
So, GetStockInfo().build_multiple(['aapl'],['2019-01-01'],['2020-01-01']) should be the form. Btw I'm taking lists of start and end dates - if it's always gonna be the same, you just use variables instead. In variable format, it should be : GetStockInfo().build_multiple(['aapl','msft'],'2019-01-01','2020-01-01')
I think you got it sorted already? But, it should be result = GetStockInfo().build_multiple(['aapl','msft'],'2019-01-01','2020-01-01'); result['aapl'].head()
|
1

You could put the dataframes in a list and return that. Or better yet, in a dictionary:

results = {}
for i in symbols:
    stock = GetStockInfo(i)

    results[i] = stock.build_df('2019-01-01', '2020-01-01')
return results

2 Comments

would this need to be in the build_df function or outside of it?
Depends on your preference I guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.