I am not sure what I am doing wrong
I am looking to merge two pd dataframes using merge_asof because I want to merge my two tables not only on dates but on the closest date found.
def get_historical_pe(data, stock):
item_data = {}
for i,j in data['Earnings']['History'].items():
item_data[j['date']]=[j['epsActual'], j['reportDate']]
df = pd.DataFrame(item_data).T
df.columns = ['EPS', 'Release date']
df.dropna(subset=['EPS'], how='all', inplace=True)
df['date2'] = df.index.astype('datetime64[ns]')
print(type(df['date2'][0]))
df.index = pd.to_datetime(df.index, format ='%Y-%m-%d')
df.loc[date.today()] = [df.loc[df.index.max(),'EPS'], 'TTM', date.today()]
stocksplit = stock.split('.')
if stocksplit[1] == 'US':
stockPricesdata = yf.Ticker(stocksplit[0])
else:
stockPricesdata = yf.Ticker(stock)
stockPrices = stockPricesdata.history(period="max")['Close']
stockPricesdf = pd.DataFrame(stockPrices)
stockPricesdf['date2'] = stockPricesdf.index.astype('datetime64[ns]')
tol = pd.Timedelta('1 day')
st = pd.merge_asof(
left=df,
right=stockPricesdf,
on='date2',
direction='nearest',
tolerance=tol
)
return st
However I am getting an error: MergeError: Incompatible merge dtype, dtype('<M8[ns]') and dtype('O'), both sides must have numeric dtype however I checker to change the type of my merge column to the same type .. I do not understand what am I doing wrong
Could you please help me. Thx you