2

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 a row of df['Coupon'] into each column of a corresponding row of the array for as many columns as the number numberofcoupon_payments = row1['months_to_maturity']/6. I get the error ValueError: could not broadcast input array from shape (1,2) into shape (1,61) in 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 proceed.

The code I am using is as follows:

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 dataframe 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 the array to look like this, if the months_to_maturity column of df has values 6,12,18:

array([[coupon     0     0      0], 
       [coupon   coupon  0      0], 
       [coupon   coupon coupon  0]])

Thank You

1
  • This may not be that hard to do but it would be much easier to understand your question if you provided a small dataframe and desired output array. Commented Jul 1, 2015 at 23:00

1 Answer 1

2

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. ]])
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. But unfortunately the matrix_of_coupons_and_facevalues prints out to be [[ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] ..., [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.]]
The row['coupon'] values don't see to be getting added.
i don't know what to tell you, it works on my simple example, can you work build up your data from here?
Yeah, the logic makes sense. And sorry, what do you mean by "can you work build up your data from here?"
i don't know, i tried with your data and it still works, are you sure you're whole matrix is zero? and not just the rows that you can see? try this: matrix_of_coupons_and_facevalues[matrix_of_coupons_and_facevalues[:,0] != 0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.