ValueError:无法将大小为5的序列复制到维度为2的数组轴 [英] ValueError: cannot copy sequence with size 5 to array axis with dimension 2

查看:1323
本文介绍了ValueError:无法将大小为5的序列复制到维度为2的数组轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用numpy 1.7.1,以下代码工作并产生如下所示的结果,

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)

但是,在numpy 1.9.2中,相同的输入会产生如下异常,

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

"ValueError:无法将大小为5的序列复制到维度为2的数组轴上"

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

需要知道numpy不支持此操作的原因或两个版本中都可以使用的常规修补程序的原因.在两个版本的numpy中,我都希望获得与1.7.1中相同的输出.

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.

推荐答案

我能够用numpy 1.9.2重现您的问题.似乎numpy正在尝试执行vstack.当形状相同时.我尝试了以下方法,但效果很好.

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)

这篇关于ValueError:无法将大小为5的序列复制到维度为2的数组轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆