Python Forum
2-dataframe, datetime lookup problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: 2-dataframe, datetime lookup problem (/thread-36196.html)



2-dataframe, datetime lookup problem - Mark17 - Jan-27-2022

Please see the attached screenshot:

[attachment=1547]

What I'm trying to do is go down df['Period_End_Date'], take those dates and find the respective values in spx_prices['Adj Close'], and put that value in the a new column df['Price'] for the same date. This seems like a straightforward .loc[] or .iloc[] problem but I get a lot of KeyError or I don't even remember what else.

Here's the code so far (with a lot of extra stuff for unsuccessful debugging):

from datetime import date
from datetime import timedelta
import pandas as pd
import yfinance

friday_dates = []
resultant_date = date(2001,5,25)
previous_month = 0

while resultant_date < date(2022,1,8):
    if previous_month != resultant_date.month:
        if len(friday_dates) == 0:
            friday_dates.append(resultant_date)
            previous_month = resultant_date.month
        else:
            friday_dates.append(resultant_date-timedelta(7))
            previous_month = resultant_date.month
    resultant_date = resultant_date + timedelta(7)

del friday_dates[0] #got duplicate of first date

#print(friday_dates)

df = pd.DataFrame(friday_dates,columns=['Period_End_Date'])
#df.columns[0] = 'Period End Date'
#print(df)

spx_prices_raw = yf.download('^gspc',start='2001-5-25')
spx_prices = spx_prices_raw.drop(['Open','High','Low','Close','Volume'],axis=1)

#print(spx_prices.head())

#df = pd.to_datetime(df['Period_End_Date'])  <----wrong
df['Period_End_Date'] = pd.to_datetime(df['Period_End_Date'])
print()
print('df.info() :')
print(df.info())
print('df.head() :')
print(df.head())
print()
print('spx_prices.info() :')
print(spx_prices.info())
print('spx_prices.head() :')
print(spx_prices.head())
print('Shapes of df and spx_prices are {} and {}, respectively'.format(df.shape,spx_prices.shape))
#df.head()
#print(type(spx_prices))
#for i in df['Period_End_Date']:
    #print(spx_prices.iloc[i])
    #df['Price'] = spx_prices.loc[i]
Something like the last three (commented out) lines, in particular, is my attempt to get dates from df (which has dates of last Friday for each month), look up in spx_prices (which has dates for every trading day), Thanks for any help you can give!