I need to extract the semester of a given date and I ended up with this code:
df['semester']= df.my_date.dt.year.astype(str) '-S' np.where(df.my_date.dt.quarter.gt(2),2,1).astype(str)
Can someone explain to me how this part works:
np.where(df.my_date.dt.quarter.gt(2),2,1)
CodePudding user response:
Check numpy.where
if condition is True
, here Series.dt.quarter
is greater by Series.gt
like 2
set 2
else 1
.
So it is same like:
np.where(df.my_date.dt.quarter > 2,2,1)