Reputation: 619
I have a dataframe with about two years worth of dates. I am trying to create a new column that will indicate if the date has earnings reported on that day. How can I do this?
DF
Date Stock
2018-02-15 100
.
.
.
2018-04-02 122
2018-04-03 119
2018-04-04 120
My attempt I looked up the earning dates then I tired to join but failed.
df = pd.DataFrame(columns=["Earnings_Date", "Earnings Reported"],
data=[['05/01/2018','Yes'],['02/15/2018','Yes'],['10/31/2017','Yes'],['08/01/2017','Yes']])
Ideally, I would like my DF to look like
DF
Date Stock Earnings Reported
2018-02-15 100 Yes
.
.
.
2018-04-02 122 No
2018-04-03 119 No
2018-04-04 120 No
Upvotes: 1
Views: 68
Reputation: 153460
No need for join, merging. Just check to see if Date is in the Earnings_Date column.
df['Earnings Report'] = np.where(df['Date'].isin(mydf['Earnings_Date']),'Yes','No')
Where mydf is the Earnings Report dataframe, and df is the original stock dataframe.
Date Stock Earnings Report
0 2018-02-15 100 Yes
1 2018-04-02 122 No
2 2018-04-03 119 No
3 2018-04-04 120 No
Upvotes: 2