0

I'm trying to convert the following code's output into 2 lists, dates and prices, which contain the dates and prices that are outputted as a dtype:float64

from pandas_datareader import data
def get_stock_data(ticker, start, end):
    stock_data = data.DataReader(ticker, start=start, end=end, data_source='yahoo')['Close']
    return stock_data

print(get_stock_data('TSLA', '2021-01-01', '2021-01-31'))

stock_data.tolist() gives me the prices but how do I get the dates?

This could be any arbitrary set of start and end dates so I don't want to manually create a list for one test case.

2 Answers 2

0

Assuming the return is a Pandas series and the dates are the index stock_data.index.tolist().

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

1 Comment

This might help you convert to regular datetime object: stackoverflow.com/questions/34386751/…
0

Your code needs to be modified a bit to get two different lists of dates and prices. I have converted the stock_data into a pandas dataframe and extracted the lists of dates and prices from the dataframe as follows:

from pandas_datareader import data
def get_stock_data(ticker, start, end):
    stock_data = data.DataReader(ticker, start=start, end=end, data_source='yahoo')['Close']
    df = pd.DataFrame(stock_data)
    df.reset_index(inplace=True)
    dates = df["Date"].astype(str).tolist()
    prices = df["Close"].tolist()
    
    return dates, prices

get_stock_data('TSLA', '2021-01-01', '2021-01-31')

Comments

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.