pythonpandasdatedatetimedat-protocol

Sum the value of a dictionary based on the difference between two columns in a dataframe and divide the first column by two - Python


I have a dataframe and a dictionary

 Start_date     End_Date

1 2019-01-16    2019-05-28  
2 2018-06-05    2018-07-31  
3 2019-02-11    2019-04-14  
{'HDD': {'2015-01': 477.6,
  '2016-01': 429.0,
  '2017-01': 593.8,
  '2018-01': 372.1,
  '2019-01': 502.8,
  '2015-02': 457.4,
  '2016-02': 377.6,
  '2017-02': 369.8,
  '2018-02': 469.8,
  '2019-02': 395.5,
  '2015-03': 325.2,
  '2016-03': 370.8,
  '2017-03': 266.1,
  '2018-03': 392.9,
  '2019-03': 297.3,
  '2015-05': 24.2,
  '2016-05': 97.4,
  '2017-05': 88.5,
  '2018-05': 41.4,
  '2019-05': 118.1,
  '2015-06': 0.0,
  '2016-06': 0.0,
  '2017-06': 0.0,}}

The output crate a new column value which is the sum of the dictionary'value (counting the months between the start and end date).

 Start_date     End_Date    Value

1 2019-01-16    2019-05-28  760
2 2018-06-05    2018-07-31  803
3 2019-02-11    2019-04-14  200

Problem is here--> I want to divide by 2 the value of the HDD of the start_date month, if the day of the start_date is above 15 and divide the value of the end_date if the day of the end_date is below 28. The value between the two date will not be divided by 2, only the value of the month of the start/end_date. My code works for a part, it can divide the end_date by 2 but for the start_date it takes the entire value of HDD.

from datetime import datetime, date, time
import calendar
def get_sum_values(start_date, end_date, dictionary,start_middle=15, end_middle=28):
    tot= 0
    j = 1
    i=1
    difference = (end_date.year - start_date.year) * 12 + (end_date.month - start_date.month)
    for key in dictionary['HDD'].keys():
        if datetime.strptime(key, '%Y-%m')>=start_date and datetime.strptime(key, '%Y-%m')<=end_date:
            if (i==0 and start_date.day >= start_middle ) or (j==end_date.month and end_date.day<=end_middle):
                tot+=dictionary['HDD'][key]/2
            else:
                tot+=dictionary['HDD'][key]
        #if start_date.dt.day <= start_middle or end_date.dt.day>=end_middle:
                #-dictionary['HDD'][key][end_date]/2
            i+=1
            j+=1
    return tot

gaz['HDD'] = gaz.apply(lambda row: get_sum_values(row['Start_Date'], row['End_Date'],hdd_dict), axis=1)

I hope it's clear. Thanks a lot for your help :).


Solution

  • If you data is not too big, you can use apply for this:

    lookup = pd.DataFrame(d)
    lookup.index=pd.to_datetime(lookup.index).to_period('M')
    
    df['Value'] = df.apply(lambda x: lookup.loc[x['Start_date']: x['End_Date'], 'HDD'].sum(), axis=1)
    

    Output:

      Start_date   End_Date   Value
    1 2019-01-16 2019-05-28  1313.7
    2 2018-06-05 2018-07-31     0.0
    3 2019-02-11 2019-04-14   692.8