I have two dataframes: Data & Positions.
Data has multiindex: 'Date' and 'Symbol'. Positions has a single index: 'Date' (and has a column with the label 'Symbol'). Both 'Date'-s are DatetimeIndexes.
I want to fetch the 'Open' value from Data to Positions for all the dates (indexes) in Positions.
If I try the following:
positions['Open_price'] = data.loc['1997-02-10','AA'].Open
I get the 'Open' value correctly for the 'AA' Symbol on 1997-02-10 for the 'Open_price' field for the whole Positions dataframe. However, my goal is to get 'Open_price' for the relevant Date (index) and Symbol (column value).
So I try this:
positions['Open_price'] = data.loc[positions.index,positions.Symbol].Open
Then I get an error:
KeyError: "None of [DatetimeIndex(['1999-01-01', ...\n '2018-07-30'],\n
dtype='datetime64[ns]', length=7151, freq='D')]
are in the [index]"
NOTE: It is important that Data's 'Date' index has missing values (weekends). Positions' 'Date' index has no missing dates.
How can I make this work?
IMPORTANT:
Accepted answer works but I needed to upgrade pandas version 0.20.x to 0.23.4!