Question: import pandas as pd import os # TASK - # 08/01/2022 - to 08/15/2022 # symbols GLD, GOOG, SPY # print max() Adj Close price.

import pandas as pd import os # TASK - # 08/01/2022 - to 08/15/2022 # symbols GLD, GOOG, SPY # print max() Adj Close price. for each symbol # # -- Files already downloaded ... in data/ subdirectory ## print( f" last 5 values: { df.head(5) }") def symbol_to_path( symbol, base_dir=os.path.join(".", "data" )): """Return CSV file path given ticker symbol.""" return os.path.join(base_dir, f"{symbol}.csv") def test_run(): """some commentary what ever you want hello hello hello """ symbols = ['GLD', 'GOOG', 'SPY','AAPL','AMZN','CVNA','IBM','OPEN','W','XOM'] start_date = '2020-01-02' end_date = '2020-12-30' dates = pd.date_range(start_date, end_date) # may need we will see df2 = pd.DataFrame(index=dates) # empty data frame - maybe need later ... # read in a series of Symbols - for loop for symbol in symbols: dataname = symbol_to_path(symbol) df_temp = pd.read_csv(dataname, index_col ="Date", parse_dates=True, usecols=['Date', 'Adj Close'], na_values=['NAN']) df_temp =df_temp.rename(columns={'Adj Close': symbol}) df_temp=df_temp.join(df_temp, how="left") return df_temp # print the max() value of the value between the dates (HINT - using dates as index may be good( for symbol in symbols: symbolfile_name = symbol_to_path(symbol) # print(f"getting file -->{symbolfile_name}") # https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html # df = pd.read_csv( symbolfile_name ) # Easier to restrict data according to Date, with selection/Slicing df = pd.read_csv( symbolfile_name, index_col='Date' ) # > right aligns in 4 a 4 space containter print(f"--> {symbol:>4s}.max() --> {df.loc[start_date:end_date,'Adj Close'].max():8.4f}") # a note about date types - loc returns a slice of a frame which is a Series. not # --> another data frame. print(f"-->\t\tdata type of df.loc[...] = {type(df.loc[start_date:end_date,'Adj Close'])}"); if __name__ == "__main__": print("HERE") test_run()I don't know what is wrong in # read in a series of Symbols - for loop I.
Task 1: modify as follows: def get_StockDataHD( symbol, dates ) 1) input parameter takes a list of symbols instead of a single symbol: test: ['SPY', 'CVNA','XOM', 'AMZN', 'GLD'] use date range: 2020-03-31 2) add a loop that adds symbols iteratively to the frame using join. Example Output (you should reproduce this output): Task 1: Suggested Steps 1. for symbol in symbols: to read in each symbol file 2. use rename() to rename 'Adj Close' column (already done) by symbol string. 3. next you use a left join() that's the default it simply retains the keys (index) from the left frame only, so you get the full range of the left frame. (left frame is the frame that's on the left of "." which is df in the below join: df = df.join( df_temp, how = left' ) df_temp is on the right. 4. make sure the return of the df is not inside the for loop. save frame as csv file name it task1.csv
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
