clean crsp pandas code example
Example: clean crsp pandas
CRSP = CRSP[CRSP['SHRCLS'].isin(['NaN', 'A'])]
CRSP = CRSP[CRSP['SHRCD'].isin(['10', '11'])]
CRSP['RET'] = CRSP['RET'].replace(['C','B'],np.nan)
CRSP['RET'] = CRSP['RET'].astype('float')
mask = CRSP['RET'] > -50
CRSP = CRSP[mask]
CRSP['PRC'] = CRSP['PRC'].abs()
CRSP['market_value'] = ((CRSP['PRC']/CRSP['CFACPR'])*(CRSP['SHROUT']*CRSP['CFACSHR'])).shift(1)
mask_z = CRSP['SICCD'] == 'Z'
CRSP['SICCD'] = CRSP['SICCD'][-mask_z]
CRSP['SICCD'] = CRSP['SICCD'].dropna().astype(int)
CRSP = CRSP[~CRSP['SICCD'].between(6000,6999)]
CRSP['date'] = pd.to_datetime(CRSP['date'])
CRSP['year'] = pd.DatetimeIndex(CRSP['date']).year
CRSP['month'] = pd.DatetimeIndex(CRSP['date']).month
CRSP.drop(['SHRCD','SHRCLS','PRC','SHROUT','CFACPR','CFACSHR'], axis=1, inplace = True)