Python: Datetime to season

You can use a simple mathematical formula to compress a month to a season, e.g.:

>>> [month%12 // 3 + 1 for month in range(1, 13)]
[1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 1]

So for your use-case using vector operations (credit @DSM):

>>> temp2.dt.month%12 // 3 + 1
1    3
2    3
3    3
4    4
5    4
6    4
7    4
8    4
Name: id, dtype: int64

Leave a Comment