简体   繁体   中英

Python dates filling in blanks, pandas dataframe

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM