pandas: Buffer has wrong number of dimensions (expected 1, got 2) when using hstack

334 Views Asked by At

I am trying to hstack some values using np. However I get the following error:

Buffer has wrong number of dimensions (expected 1, got 2) 

The line in which I stack is:

s= np.hstack((np.array([cell_name, freq]).reshape((1,-1)),pred,y1_a,y1_b,y1_c,y2_a,y2_b,y2_c,y3_a,y3_b,y3_c,df[output_name].iloc[i].values.reshape(1,-1)))

I checked the shape of every element in the array, they all have the same first dimension of 1. Any ideas?

ps: the length of the dataframe I am trying to work on is 2, and this stacking is done in for loop as follows:

for i in range(df.values.shape[0]):

    s= np.hstack((np.array([cell_name, freq]).reshape((1,-1)),pred,y1_a,y1_b,y1_c,y2_a,y2_b,y2_c,y3_a,y3_b,y3_c,df[output_name].iloc[i].values.reshape(1,-1)))

1

There are 1 best solutions below

0
On

You have to debug in more detail. You can try to hstack as long as possible. Somewhere the input has to be of dimension two.

I think you could try this.

for i in reange(df.shape[0]):
    my_list = [np.array([cell_name, freq]).reshape((1,-1)), pred, y1_a, y1_b, y1_c, y2_a, y2_b, y2_c, y3_a, y3_b, y3_c, df[output_name].iloc[i].values.reshape(1,-1)]
    for j in range(len(my_list)):
        print(f'run: {j}')
        print(my_list[j].shape)
        tup = tuple(my_list[:j])
        s = np.hstack(tup)

If this is not working, you should publish the numpy arrays. Maybe you can create a minimal example where it is not working.