I have two DataFrames. The first, df1, has historical time series data for a variety of tickers with a DateTime index that looks like this:
ABC DEF XYZ
2011-06-06 10.00 10.00 10.0000
2011-06-17 10.00 10.00 10.0000
2011-06-21 10.00 10.00 10.0000
2011-06-22 10.00 10.00 10.0000
2011-06-23 10.00 10.00 10.0000
2011-06-24 10.00 10.00 10.0000
2011-06-30 10.00 10.00 10.0000
2011-07-11 10.00 10.00 10.0000
The second, df2, has three columns; a Start_Date, End_Date, and Ticker. Both the Start_Date and End_Date are in datetime format:
End_Date Start_Date Ticker
0 2011-06-27 2011-06-22 ABC
1 2011-06-30 2011-06-17 DEF
2 2011-06-25 2011-06-18 XYZ
I want to create a third DataFrame, df3, using the following code
df4 = df.copy()
df4.lock[:] = np.nan
Between df2['Start_Date'] and df2['End_Date'] I want to populate df3 rows with 1.00 and leave the other rows as np.nan.
I've tried to create a function and also to iterate over df2.
def pos():
position = 1
for i in df2['Ticker']:
df3.at[df2['Start_Date'], i] = position
return pos
or
def pos():
position = 1
for index, row in df2.iterrows:
df3.at[index, row['Start_Date']] = position
return pos
The resulting df3 would look like this:
ABC DEF XYZ
2011-06-06 NaN NaN NaN
2011-06-17 NaN 1.0 NaN
2011-06-21 NaN 1.0 1.0
2011-06-22 1.0 1.0 1.0
2011-06-23 1.0 1.0 1.0
2011-06-24 1.0 1.0 1.0
2011-06-30 NaN 1.0 NaN
2011-07-11 NaN NaN NaN
2011-07-13 NaN NaN NaN
2011-07-14 NaN NaN NaN
I am not having much luck with either. What is the best way to do this?
Thanks in advance