I am trying to write a code where it separates the data in months with the month_changes. The Values and Val_dates are corelated, Val_dates are supposed to be the matching dates for the Values indexes. So [100,'2015-11-01 01:03:00'],[123, '2015-11-08 12:56:00']...... Each row in the multidimensional array is suppose to represent a single month so the first row [100,123,135.3,139.05,156.08,163.88,173.72] is for the November of 2015 and the sixth row [100,106] is for February of 2016 etc. I am trying to write a function where it iterates through the month rows as well as the number of valid indexes for that month. So for November of 2015 there are 4 dates that have the same month and year, ['2015-11-01 01:03:00', '2015-11-08 12:56:00', '2015-11-11 02:30:00', '2015-11-14 04:23:00'] so since the 4th index is the last on the first row it will output 139.05 which is the 4th index in [100,123,135.3,139.05,156.08,163.88,173.72]. For the rows that dont have any date matches it will just output 0. How could I get the Expected Output?
import numpy as np
#[23,10,3,12,5,6]
Values = np.array([[100,123,135.3,139.05,156.08,163.88,173.72],
[100,110,113,126.56,132.89,140.86],
[100,103,115.36,121.13,128.4],
[100,112,117.6,124.66],
[100,105,111.3],
[100,106]])
Val_dates= ['2015-11-01 01:03:00', '2015-11-08 12:56:00', '2015-11-11 02:30:00', '2015-11-14 04:23:00', '2016-02-11 02:00:00', '2016-02-15 15:00:00']
month_changes = ['2015-11-01 00:00:00', '2015-12-01 00:00:00', '2016-01-01 00:00:00',
'2016-02-01 00:00:00', '2016-03-01 00:00:00']
format_month = np.sort(month_changes)
def Monthly_Pnls(index, Values):
# Digitize
digit_month = np.digitize(index, format_month)
Monthly_PnL = np.bincount(digit_month, weights=PnL)
Monthly_PnL= np.around(Monthly_PnL[1:len(format_month)],1)
print(Monthly_PnL)
return Monthly_PnL
Monthly_Pnls(Val_dates, month_changes)
Expected Output:
[139.05,0,0,0,106,0]
Max Inputs:
Values = np.array([[123. 104.55 107.6865 105.53277 110.8094085
117.45797301]
[85. 87.55 85.799 90.08895 95.494287]
[103. 100.94 105.987 112.34622]
[ 98. 102.9 109.074]
[105. 111.3]
[106.]])
Expected Output:
[123, 0.0, 0.0, 105.0, 0.0]
len(Values)-> 7 andlen(Val_dates)-> 6