-1

Here is the table specified as df:

id ticker date
1 PLTR 2022-01-07
2 GME 2022-01-06
3 AMC 2022-01-06
4 GOOD 2022-01-07
5 GRAB 2022-01-07
6 ALL 2022-01-06
7 FOR 2022-01-06

I want to have something like this:

id ticker date Price
1 PLTR 2022-01-07 $16.56
2 GME 2022-01-06 $131.03
3 AMC 2022-01-06 $22.46
4 GOOD 2022-01-07 $24.76
5 GRAB 2022-01-07 $6.81
6 ALL 2022-01-06 $122.40
7 FOR 2022-01-06 $21.26

I tried df['Price'] = yf.download(df['ticker'],df['date'])['Close'] using the yahoo finance tool but received an error: AttributeError: 'Series' object has no attribute 'split'

I also tried the pandas_datareader (imported as web), got the same error: df.assign(Price=web.DataReader(list(df.ticker('\n')), 'yahoo', list(df.date)))['Close']

Any advice/ideas what I am doing wrong?

smithy
  • 1
  • 1
  • The problem as you have written the code is that ```d[f'ticker]``` and ```df['date]``` produce a pandas series object. You need to increment through the df on a row by row basis to extract the ticker price for a item on a specific data. – itprorh66 Jan 09 '22 at 17:10
  • Can you add some sample data so your question is [reproducible](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples)? – Nick ODell Jan 09 '22 at 18:21
  • The dataframe is exactly the first table – smithy Jan 09 '22 at 18:50

1 Answers1

0
import pandas as pd
import pandas_datareader.data as web

tickers = list(df.ticker)

prices = ( web.DataReader(tickers, data_source='yahoo', start=df.date.min().date(), end=df.date.max().date() )['Close']
   .reset_index()
   .melt(id_vars=['Date'])
   .rename(columns={'Symbols':'ticker', 'Date':'date'})
)

prices:
date ticker value
0 2022-01-06 00:00:00 PLTR 16.74
1 2022-01-07 00:00:00 PLTR 16.56
2 2022-01-06 00:00:00 GME 131.03
3 2022-01-07 00:00:00 GME 140.62
4 2022-01-06 00:00:00 AMC 22.46
5 2022-01-07 00:00:00 AMC 22.99
6 2022-01-06 00:00:00 GOOD 25.03
7 2022-01-07 00:00:00 GOOD 24.76
8 2022-01-06 00:00:00 GRAB 6.65
9 2022-01-07 00:00:00 GRAB 6.81
10 2022-01-06 00:00:00 ALL 122.4
11 2022-01-07 00:00:00 ALL 125.95
12 2022-01-06 00:00:00 FOR 21.26
13 2022-01-07 00:00:00 FOR 20.19

Now merge them:

df.merge(prices, on=['ticker','date'], how='left')
id ticker date value
0 1 PLTR 2022-01-07 00:00:00 16.56
1 2 GME 2022-01-06 00:00:00 131.03
2 3 AMC 2022-01-06 00:00:00 22.46
3 4 GOOD 2022-01-07 00:00:00 24.76
4 5 GRAB 2022-01-07 00:00:00 6.81
5 6 ALL 2022-01-06 00:00:00 122.4
6 7 FOR 2022-01-06 00:00:00 21.26
kleynjan
  • 108
  • 5