Skip to content
Advertisement

Python: Datetime to season

I want to convert a date time series to season, for example for months 3, 4, 5 I want to replace them with 2 (spring); for months 6, 7, 8 I want to replace them with 3 (summer) etc.

So, I have this series

JavaScript

and this is the code I have been trying to use, but to no avail.

JavaScript

and

JavaScript

Neither solution works, specifically in the first implementation I receive an error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

While in the second is a large list with errors. Any ideas please? Thanks

Advertisement

Answer

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

JavaScript

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

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement