1

I'm trying to turn this stock data I got from yfinance into a dataframe. It won't let me call the dates or Adjusted close so I can't merge it into other dataframes I have. This looks simple but i'm really stuck. Hope you can help, thanks!

price_history_i = sp.get_price_history('tickername', '2019-12-31', '2020-09-01')['Adj Close']
price_history_a = sp.get_price_history('tickername', '2019-12-31', '2020-09-01')['Adj Close']
price_history_c = sp.get_price_history('tickername', '2019-12-31', '2020-09-01')['Adj Close']

df = pd.DataFrame([price_history_i, price_history_a, price_history_c])
average_price = df.mean()
print(average_price)

Output:

Date
2019-12-31     9.863333
2020-01-02     9.903333
2020-01-03     9.883333
2020-01-06     9.883333
2020-01-07     9.883333
                ...    
2020-08-25    10.133333
2020-08-26    10.173333
2020-08-27    10.183333
2020-08-28    10.206667
2020-08-31    10.203334
Length: 169, dtype: float64

As you can see, the Adj Close isn't listed above the price. I'm using a simple function that I made in the backround that lets me download yfinance info quicker called get_price_history

import yfinance as yf



def get_price_history(ticker, sdate, edate):
    data = []
    data = yf.download(ticker, start=sdate, end=edate)
    return (data)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • "Adj Close isn't listed above the price" ... Because you didn't name that mean column anything, perhaps? You can use `iloc` to access unnamed columns – OneCricketeer Sep 09 '20 at 18:04
  • ```TypeError: mean() got an unexpected keyword argument 'columns'``` when I tried to add the column – anony123454323 Sep 09 '20 at 18:06
  • Right... [columns isn't a valid parameter](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.mean.html). I meant [rename](https://stackoverflow.com/questions/19758364/rename-specific-columns-in-pandas) – OneCricketeer Sep 09 '20 at 18:09
  • Any chance you could give me an example please? – anony123454323 Sep 09 '20 at 18:12
  • `df.mean()` returns a Series. You can rename parts of Series - https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.rename.html – OneCricketeer Sep 09 '20 at 18:15
  • ohh i see. So the series is names correctly then. But How come when I to_frame() it I don't get a dataframe? – anony123454323 Sep 09 '20 at 18:19
  • I'm not seeing that in the question, but one suggestion I have is to make your initial dataframe two columns `[ticker, adj_close]` rather than trying to merge three dataframes horizontally. Otherwise, looks like you're finding the mean of different tickers – OneCricketeer Sep 09 '20 at 22:24

1 Answers1

1

Workaround:

pd.DataFrame(df.mean(), columns = [df.T.columns[0]])

considering that df is laying horizontal (I think it is) and that the name of the columns (are "adj close")

aerijman
  • 2,522
  • 1
  • 22
  • 32
  • It looks like the Date and Adj Close aren't on the same line. Like the Adj Close fits well above the prices whiles the Date is right above the dates. I'm confused because It's still not in a dataframe. The reason I care so much is because I'm trying to align multiple dataframes with the same dates and I need to calculate everything together. – anony123454323 Sep 09 '20 at 19:55
  • Date is `Index` while adj... is a `column`. You see the correct thing and is supposed to be like this – aerijman Sep 09 '20 at 19:57
  • I see!!! So how can I turn the date from an index into a column and have the index = 0,1,2,3...etc – anony123454323 Sep 09 '20 at 20:04