Search code examples
python-2.7pandasjupyter-notebookpandas-datareader

Maths Operations on Columns from Different Data Frames


I have two data frames, imported through Pandas from Fama French and Yahoo. I am trying to compare column values from the two data frames (more specifically, subtract one from the other), but a value error occurs whenever I try doing so. The data frames have different indexing and I don't know how to take this factor into account (I'm quite new to python & pandas).

Here is the code in question:

start, end = dt.datetime.now()-dt.timedelta(days=60*30), dt.datetime.now()
f = data.DataReader('F-F_Research_Data_Factors', 'famafrench',  start, end)[0]
s = data.get_data_yahoo('aapl', start, end)
s = s.resample('M', how='last')
s['returns'] = s['Adj Close'].pct_change()

Ideally, I would like to create a series with row values = f['RF'] - s['returns']

Any help would be much appreciated.


Solution

  • Convert f.index

    f.index = f.index.to_datetime() + pd.offsets.MonthEnd()
    
    f['RF'] - s['returns']