I have the following pandas data frame with a date column. How can I add a column specifying which half year the date belongs to?
>Solution :
Convert dates to datetimes and then use numpy.where with compare for less or equal:
df['date'] = pd.to_datetime(df['date'], dayfirst=True)
df['half year'] = np.where(df['date'].dt.month.le(6), 'H1', 'H2')
print (df)
date half year
0 1993-09-09 H2
1 1993-09-11 H2
2 1994-01-23 H1
3 1993-03-18 H1
Solution without numpy with change mask for greater like 6, add 1 and convert to strings:
df['date'] = pd.to_datetime(df['date'], dayfirst=True)
df['half year'] = 'H' + df['date'].dt.month.gt(6).add(1).astype(str)
print (df)
date half year
0 1993-09-09 H2
1 1993-09-11 H2
2 1994-01-23 H1
3 1993-03-18 H1
