0

I am a complete beginner in Python and it is my first question on Stackoverflow. I have tried numerous tutorials on youtube + some additional google searching, but havent been really able to completely solve my task. Briefly putting it below asf:

We have a dataset of futures prices (values) for next 12-36 months. Each value corresponds to one month in future. The idea for the code is to have an input of following:

  1. starting date in days (like 2nd of Feb 2021 or any other)
  2. duration of given days (say 95 or 150 days or 425 days)
  3. The code has to calculate the number of days from each given month between starting and ending date (which is starting + duration) and then to use appropriate values from corresponding month to calculate an average price for this particular duration in time.

Example: Starting date is 2nd of Feb 2021 and duration is 95 days (end date 8th of May). Values are Feb - 7750, Mar - 9200, April - 9500, May is 10100.

I have managed to do same in Excel (which was very clumsy and too complicated to use on the daily basis) and average stands for around 8949 taking in mind all above. But I cant figure out how to code same "interval" with days per month in Python. All of the articles just simply point out to "monthrange" function, but how is that possible to apply same for this task?

Appreciate your understanding of a newbie question and sorry for the lack of knowledge to express/explain my thoughts more clear.

Looking forward to any help relative to above.

  • have a look at `timedelta` module in the [datetime](https://docs.python.org/3/library/datetime.html) package. – Yogaraj Jan 03 '21 at 13:48

2 Answers2

2

You can use dataframe.todatetime() to constuct your code. If you need further help, just click ctrl + tab within your code to see the inputs and their usage.

Ling Huai
  • 21
  • 2
0

You can try the following code.

  1. The input_start_date() function will input the start date, and return it when called.

  2. After we have the start date we input the duration of days.

  3. Then we simply add them using timedelta

  4. For the Distribution of days in the month : SO - @wwii

import datetime
from datetime import timedelta

def input_start_date():
    YEAR = int(input('Enter the year : '))
    MONTH = int(input('Enter the month : '))
    DAY = int(input('Enter the day : '))

    DATE = datetime.date(YEAR, MONTH, DAY)

    return DATE

# get the start date:
Start_date = input_start_date()

# get the Duration
Duration = int(input('Enter the duration : '))

print('Start Date : ', Start_date)
print('Duration :', Duration)

# final date.
Final_date = Start_date + timedelta(days=Duration)
print(Final_date)

# credit goes to @wwii -----------------------
one_day = datetime.timedelta(1)
start_dates = [Start_date]
end_dates = []
today = Start_date
while today <= Final_date:
    tomorrow = today + one_day
    if tomorrow.month != today.month:
        start_dates.append(tomorrow)
        end_dates.append(today)
    today = tomorrow

end_dates.append(Final_date)
# -----------------------------------------------

print("Distribution : ")
for i in range(len(start_dates)):
    days = int(str(end_dates[i]-start_dates[i]).split()[0]) + 1
    print(start_dates[i], ' to ', end_dates[i], ' = ', days)

print(str(end_dates[0]-start_dates[0]))

'''
Distribution : 
2021-02-02  to  2021-02-28  =  27
2021-03-01  to  2021-03-31  =  31
2021-04-01  to  2021-04-30  =  30
2021-05-01  to  2021-05-08  =  8
'''
XXDIL
  • 220
  • 2
  • 10
  • Thanks Mohammed, but how to code to define the number of each day per month to further multiply it by respective values? Like in my example to have 26 days in Feb, 31 d in Mar, 30 days in April and 8 days in May. Then to use respective values (which are different for each month) to further get an average value for the necessary duration in time? Thanks – Nightguestt Jan 03 '21 at 17:25
  • So you want each month distribution? (Feb=26, Mar=31, Apr=30, May=8). – XXDIL Jan 03 '21 at 17:29
  • Yes, like explained above. I need the code to define number of days per each month within a given interval of duration (whatever it will be 95, 250 or 400 days). Otherwise, it wont be possible to correctly estimate (reach an ending goal) the average value in accordance with days spent. Thanks for your advices, which are really appreciated! – Nightguestt Jan 03 '21 at 18:51
  • Mohammed, thanks a lot for above, which actually works fine! Much appreciated your time and efforts! I am still trying to figure out how can I use these output number of days per month for further last multiplication ? As they are not a variable or a dictionary I am struggling with it. Basically, the only last thing I need to do is to multiply each months days per month with respective value for this month (from a list) and calculate a .mean() out of it. Also another thing is that it seems each month's output "looses" 1 day. How to avoid it? – Nightguestt Jan 04 '21 at 10:59
  • Found a solution for an extra day by just converting last variable "days" into an integer and adding one. Thanks! Still trying to figure out how to continue using these output numbers in the further needed calcs. – Nightguestt Jan 04 '21 at 11:05
  • Could you clarify on how you want to multiply these values? – XXDIL Jan 04 '21 at 11:09
  • I would need 27 d of Feb to be multiplied by Feb value of 7750, 31 d of Mar x Mar value 9200, 30 d of April x April Value 9500, 8 d of May x May value 10200 and to get a mean of above equal to 8949. Same is needed to estimate a value of an asset in accordance with its actual day by day usage. Months values are stored in csv file of 2 column table and, I guess, the easiest way it would be to create a dictionary out of it or whatever you believe better to ? Thanks in advance. – Nightguestt Jan 04 '21 at 17:16
  • So now that you can get the value of day in terms of int, you can also store it in an array in such a way that the index of the array correspond to the month. After you have put the values into the array [0, 27, 31, 30, 8, 0, 0, 0, 0, 0, 0, 0] then you can individually multiply the month values with it. Its not very difficult, give it a try yourself. – XXDIL Jan 04 '21 at 18:08
  • Thanks, will def try with arrays, but I dont know how it will be working if duration is more than 12 months. In any case, thanks for your help and will keep on trying. – Nightguestt Jan 05 '21 at 10:56
  • Even if the duration is more than 12 months the array will be fine as you will just need to add it to the specific index. say may comes twice 1st it will contain 31 then just add 31 again making it 62. Then you can multiply it with the month value. – XXDIL Jan 05 '21 at 11:04
  • But the values of months are not static. Feb 21 is not equal to Feb 22 in value and in terms of duration over 365 days, it will have 62 days, but with different Feb values. Still, I've the concept and will try to build something. Thanks! – Nightguestt Jan 05 '21 at 21:32
  • Hey, still having some issues with my code. As per your initial one, it was "overwriting" final function every time and was just printing it. Instead, I am trying to get some list/array with elements for further adding of zeros and multiplication. But I dont get why it keeps on overwriting and does extend and new list variable, I have created in the same loop. Thanks for your help! for i in range(len(start_dates)): days = str(end_dates[i]-start_dates[i]).split()[0] result = [] final = [end_dates[i].month, int(days) + 1] result = result.extend(final) result – Nightguestt Jan 07 '21 at 12:39