ValueError while inserting data into a numpy array
I am trying to insert data from a dataframe df
into a numpy array matrix_of_coupons_and_facevalues
. However, I basically need to add the value associated with the row df['Coupon']
to each column of the corresponding row of the array for the number of columns, as a number numberofcoupon_payments = row1['months_to_maturity']/6
. I am getting an error ValueError: could not broadcast input array from shape (1,2) into shape (1,61)
on the line np.insert(matrix_of_coupons_and_facevalues, row_no+1, rowtobeadded, 0)
and I understand why, but I donβt know how to do it.
The code I am using looks like this:
matrix_of_coupons_and_facevalues = np.zeros((number_of_rows_and_columns, number_of_rows_and_columns))
rowtobeadded = np.zeros(number_of_rows_and_columns)
for (i1,row1) in df.iterrows():
numberofcoupon_payments = row1['months_to_maturity']/6
for row_no in range(int(number_of_rows_and_columns)):
for index_no in range(int(numberofcoupon_payments)):
coupon = row1['coupon']
rowtobeadded = np.full((1, numberofcoupon_payments), coupon)
np.insert(matrix_of_coupons_and_facevalues, row_no+1, rowtobeadded, 0)
Edit : The info frame df
looks like this:
months_to_maturity on_the_run_dt asset_id \
0 5 2015-07-02 00:00:00.0 00102CC07F4B02CA
1 6 2015-06-25 00:00:00.0 00102CD0FB2A023F
2 11 2015-04-02 00:00:00.0 00102CFED3C500D4
3 12 2015-06-25 00:00:00.0 00102C37122B0230
4 23 2015-03-02 00:00:00.0 00102C76082B0069
orig_iss_dt maturity_dt pay_freq_cd coupon \
0 2015-07-02 00:00:00.0 2015-12-31 00:00:00.0 NaN 0.000
1 2015-06-25 00:00:00.0 2015-12-24 00:00:00.0 NaN 0.000
2 2015-04-02 00:00:00.0 2016-03-31 00:00:00.0 NaN 0.000
3 2015-06-25 00:00:00.0 2016-06-23 00:00:00.0 NaN 0.000
4 2015-03-02 00:00:00.0 2017-02-28 00:00:00.0 2 0.500
closing_price cpn_type_cd months_to_maturity_1 FACE_VALUE
0 99.944389 FXDI 5 24000101.6
1 99.960889 FXDI 6 24000366.4
2 99.866806 FXDI 11 25000267.5
Desired output array: For example, I need an array to look as if the column months_to_maturity
df
has a value of 6
, 12
, 18
:
array([[coupon 0 0 0],
[coupon coupon 0 0],
[coupon coupon coupon 0]])
thank
source to share
This should do the trick:
df = pd.DataFrame({'months_to_maturity':[5,6,11,12,23],'coupon' : [0,0,0,0,.5]})
matrix_of_coupons_and_facevalues = np.zeros((5,5))
for i,row in df.iterrows():
matrix_of_coupons_and_facevalues[i,0:row.months_to_maturity/6] = row['coupon']
matrix_of_coupons_and_facevalues
array([[ 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. ],
[ 0.5, 0.5, 0.5, 0. , 0. ]])
source to share