Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
723 views
in Technique[技术] by (71.8m points)

python 2.7 - ValueError: cannot copy sequence with size 5 to array axis with dimension 2

using numpy 1.7.1 the below code works and produces the result as shown,

import pandas as pd
import numpy as np
d1 = pd.DataFrame({'Name': [1, 1, 1, 1, 1],'number': [1, 1, 1, 1, 1]})
d2 = pd.DataFrame({'Name': [1, 1, 1, 1, 1], 'number': [1, 1, 1, 1, 1]}) 
result =  np.array([d1,d2])

Value of result is,
array([    Name  number
0     1       1
1     1       1
2     1       1
3     1       1
4     1       1,
          Name  number
0     1       1
1     1       1
2     1       1
3     1       1
4     1       1], dtype=object)

But, In numpy 1.9.2 the same input produces exception as below,

"ValueError: cannot copy sequence with size 5 to array axis with dimension 2"

Need to know the reason that numpy not supporting this operation or some generic fix that can be used in both the version. I want the same kind of output as i get in 1.7.1, in both versions of numpy.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I was able to reproduce your issue with numpy 1.9.2. It seems that numpy is trying to do a vstack. when the shape are same. I tried the following approach and it worked.

result = np.empty(2, dtype=object)
result[:]= [d1, d2]

result
array([    Name  number
0     1       1
1     1       1
2     1       1
3     1       1
4     1       1,
          Name  number
0     1       1
1     1       1
2     1       1
3     1       1
4     1       1], dtype=object)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...