i want new column ['pred_n'] to be created based on certain condition, condition is as follows: if year is less than or equal to current year & month is less than current month, pred_n should be equal to yhatpct else it should be yhatpct_ft. trying following syntax:
if((dfyz['year_x'] < datetime.now().year) | ((dfyz['year_x'] == datetime.now().year) & (dfyz['mon'] < datetime.now().month))):
dfyz['pred_n'] = dfyz['yhat']*dfyz['pct']
else:
dfyz['pred_n'] = dfyz['yhat']*dfyz['pct_ft']
but output shows only if condition though in my data I have month and year from 2019 - 08 onwards and if i use
if ((dfyz['year_x'] < datetime.now().year) | ((dfyz['year_x'] == datetime.now().year) & (dfyz['mon'] < datetime.now().month))):
dfyz['pred_n'] = dfyz['yhat']*dfyz['pct']
elif (((dfyz['year_x'] == datetime.now().year) & (dfyz['mon'] >= datetime.now().month)) | ((dfyz['year_x'] > datetime.now().year))):
dfyz['pred_n'] = dfyz['yhat']*dfyz['pct_ft']
it gives output only for else condition
orinstead of|andandinstead of&in Python expressions. Also consider writingnow = datetime.now()and replacing all those calls in the expressions. Together, these will make your logic much easier to read (and reason about)”.now()to a definite point in time for all your comparisons). Choose the variable names wisely so the expresisons are easier to read. And yes, Python logical opertors areandornot