I aim to develop a function that creates the list target_list, see below. Target list has the following structure, it always starts with a date, '1995-01-01', then follows eleven elements representing the following eleven month of the year. After 'dec' I want a new datestring, '1996-01-01' and then comes the months as last year.
Target list below has length 27. I want the function to has the length of target_list as a parameter. If 28, one element would be added to target_list('april').
target_list=['1995-01-01','feb', 'mar', 'april', 'maj', 'jun', 'juli', 'aug', 'sep', 'okt', 'nov', 'dec', '1996-01-01',
'feb', 'mar', 'april', 'maj', 'jun', 'juli', 'aug', 'sep', 'okt', 'nov', 'dec', '1997-01-01', 'feb', 'mar']
The code below returns the list I want. Because I want a list of 27 months I loop over 3 years. That force me to remove the last nine elements of the list. I struggle to find a way to make this process automated. Perhaps I want a list of 145 months. The function must know how many elements to remove. This I haven't figured out. Any help or recommendations would be much appreciated. Of course more efficient solutions is also of great interest.
import math
from datetime import datetime
from datetime import date
def add_years(d, years):
try:
return d.replace(year = d.year + years)
except ValueError:
return d + (date(d.year + years, 1, 1) - date(d.year, 1, 1))
year=pd.to_datetime('1994-01-01')
years=math.ceil(27/12) #For 27 month I need three years. Use math.ceil to "round" upwards.
target=[]
for i in range(1,years+1): # Loop over the number of years
year=add_years(year,1) # Add one year for each loop
y=[year.strftime("%Y-%m-%d")]
z=y+ month # Concat the lists
target=target + z # Concat with target
target_list=target[:len(target)-9] # Remove the last nine elements of the list
print(target_list)
This is one solution.
def get_list(no_month):
import math
year=pd.to_datetime('1994-01-01')
years=math.ceil(no_month/12)
remove=(years*12)-no_month
target=[]
for i in range(1,years+1): # Loop over the number of years involved
year=add_years(year,1) # Add one year for each loop
y=[year.strftime("%Y-%m-%d")]
z=y+ month # Concat the lists
target=target + z # Concat with target
target_list=target[:len(target)-remove]
return target_list