I have a list of dates that I have extracted from a table:
piezas_vendidos = [ ]
for i in Piezas.select(Piezas.Fecha_fabricacion_piezas):
piezas_vendidos.append(i.Fecha_fabricacion_piezas)
print(piezas_vendidos)
['25/01/2020 12:17:22', '25/01/2020 12:17:22', '25/07/2020 12:17:22']
I would like to replace the entire date with the corresponding season of the year. How could I do it?
A solution, adapted from this answer on SOEn and simplified for readability, could be as follows:
This function goes through the start and end dates of each season and stops when the supplied day is between those dates, returning the name of the season.
The function requires that the parameter you pass to it be of type
date
. In your case, what you have are character strings, so you must first convert them to an object of typedate
, usingdatetime.strptime().date()
. You must also specify a format for this function that indicates how your string is structured (which part is the day, which is the month, the year, etc.). In this case, the format string would be:Now we can iterate over your list, converting each element to a
date
, which we pass to the functionget_season()
and collect its result to create another list with those results. With the list comprehensions syntax this boils down to one line:And the result obtained (for dates
['25/01/2020 12:17:22', '25/01/2020 12:17:22', '25/07/2020 12:17:22']
) is: