以文本方式查看主题

-  金字塔客服中心 - 专业程序化交易软件提供商  (http://222.73.7.161/bbs/index.asp)
--  高级功能研发区  (http://222.73.7.161/bbs/list.asp?boardid=5)
----  [讨论]关于array转list,转Dataframe的问题  (http://222.73.7.161/bbs/dispbbs.asp?boardid=5&id=181342)

--  作者:ly0794012132
--  发布时间:2020/7/22 22:08:02
--  [讨论]关于array转list,转Dataframe的问题
请教大佬,之前的get_indicator取出来的数字遍历后,存入到变量中,用type显示为<class \'numpy.ndarray\'>,后来用tolist()转化为list,然后我有3个len都为6的list。
然后新建一个dict,

all_data = {\'code\':code,
            \'ov\':kpzf, 
            \'ovb\':kpbl}

all_data_df = pd.DataFrame(all_data)

我想用同样长度(len)的list合成DF.
但是收到如下错误码。
> 编译错误 : ValueError
>     line : 7402
> 错误信息 : arrays must all be same length

然后我再合并DataFrame之前加了len判断,发现如下问题:
22:00:50 > <class \'numpy.ndarray\'>
22:00:50 > <class \'list\'>
22:00:50 > <class \'list\'>
22:00:50 > <class \'list\'>
22:00:50 > 0
22:00:50 > 0
22:00:50 > 6
22:00:50 > <class \'dict\'>
也就是说,如果不去真正执行的时候才会运行,那么这个时候系统会判断len不一样,因为2个遍历list长度都是0,
如果我删除DataFrame这段代码,最后在信息窗口显示如下代码:

21:48:41 > <class \'list\'>
21:48:41 > <class \'list\'>
21:48:41 > <class \'list\'>
21:48:41 > 6
21:48:41 > 6
21:48:41 > 6
21:48:41 > <class \'dict\'>
这段代码没啥问题,现在卡住了,因为编译错误导致无法编译,但是代码执行起来没啥问题,请问大佬如何解决?


--  作者:yukizzc
--  发布时间:2020/7/22 23:01:09
--  
all_data_df = pd.DataFrame(all_data)
你就在这句话前面输出all_data看下是什么数据,还有长度是多少


--  作者:ly0794012132
--  发布时间:2020/7/23 9:06:47
--  
all_data 是dict,


22:00:50 > <class \'numpy.ndarray\'>
22:00:50 > <class \'list\'>
22:00:50 > <class \'list\'>
22:00:50 > <class \'list\'>
22:00:50 > 0
22:00:50 > 0
22:00:50 > 6
22:00:50 > <class \'dict\'>

用dict可以直接把行列赋值给DataFrame。所以就用dict直接赋值。照理说是没啥问题的。
我下面用pycharm单独写了个测试代码
# 测试
list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
list3 = [9, 10,11,12]

dict1 = {\'first\': list1,
\'second\': list2,
\'third\': list3
}
print(type(list1))
print(type(list2))
print(type(list3))
print(type(dict1))
all_data_df = pd.DataFrame.from_dict(dict1)
print(all_data_df)

输出结果是
<class \'list\'>
<class \'list\'>
<class \'list\'>
<class \'dict\'>
   first  second  third
0      1       5      9
1      2       6     10
2      3       7     11
3      4       8     12

Process finished with exit code 0

这个没啥问题。(下面是金字塔的输出结果),在没有实际运行前,依然默认前两个list是空的,所以pandas没法把这3个list合并起来。
09:03:01 > <class \'numpy.ndarray\'>
09:03:01 > <class \'list\'>
09:03:01 > <class \'list\'>
09:03:01 > <class \'list\'>
09:03:01 > 0
09:03:01 > 0
09:03:01 > 6
09:03:01 > <class \'dict\'>

> 编译错误 : ValueError
>     line : 7402
> 错误信息 : arrays must all be same length


--  作者:yukizzc
--  发布时间:2020/7/23 9:30:41
--  
我意思你在金字塔里去输出这个数据看下是什么
--  作者:yukizzc
--  发布时间:2020/7/23 9:32:31
--  

除了类型,长度还有吧这个字典也给打印出来

在你转换成pandas之前


--  作者:ly0794012132
--  发布时间:2020/7/23 9:49:05
--  
09:47:08 > {\'code\': [\'SH600000\', \'SH601213\', \'SZ002111\', \'SZ002543\', \'SZ300157\', \'SZ300574\'], \'ov\': [], \'ovb\': []}
09:47:08 > <class \'dict\'>

dict打印出来是这个,前面code有股票列表,后面两个遍历get_indicator结果是空值。

--  作者:yukizzc
--  发布时间:2020/7/23 9:49:56
--  

那对呀说明你ov没有存好列表值都

你把代码贴下吧


--  作者:ly0794012132
--  发布时间:2020/7/23 10:04:38
--  
大佬,是这样的,如果我只是编译的话,通不过去的,如果我不合并成DataFrame,也就是通过编译,然后我回测运行的时候,这两个空的list就有值了,长度都是6。
这个是我把程序停止到合并DataFrame之前,因为能编译成功,所以,直接运行,那么遍历结果就在各自的list里面,这个结果是没啥毛病的。
21:48:41 > <class \'list\'>
21:48:41 > <class \'list\'>
21:48:41 > <class \'list\'>
21:48:41 > 6
21:48:41 > 6
21:48:41 > 6
21:48:41 > <class \'dict\'>
_1
源代码如下
arr_kpzf = np.append(arr_kpzf, get_indicator(stock_code,\'my_test1\',\'indicator_1\',\'\',\'1d\',1))
    arr_kpbl = np.append(arr_kpbl, get_indicator(stock_code,\'my_test2\',\'indicator_2\',\'\',\'1d\',1))
    code = np.append(code, stock_code)
    
print(type(arr_kpzf))
kpzf = arr_kpzf.tolist()                
kpbl = arr_kpbl.tolist()
code = code.tolist()              

print(type(kpzf))
print(type(kpbl))
print(type(code))
print(len(kpzf))
print(len(kpbl))
print(len(code))
all_data = {\'code\':code,
            \'ov\':kpzf, 
            \'ovb\':kpbl}
print(all_data)
print(type(all_data))
all_data_df = pd.DataFrame.from_dict(all_data)

--  作者:ly0794012132
--  发布时间:2020/7/23 10:07:09
--  
09:45:51 > [10.000007064254337, 9.950251175753518, -2.4489772880836704, 9.830102452864297, 0.0, -3.3372347621213514]
09:45:51 > [36.80165731087944, 419.09860642034806, 1.4014120744885517, 30.64964029670862, 13.93965808926841, 0.8121759640417437]
09:45:51 > [\'SH600000\', \'SH601000\', \'SZ002000\', \'SZ002000\', \'SZ300000\', \'SZ300000\']


这个是我断在合并DataFrame之前的结果,所有list都是有内容的,加上DataFrame的时候,因为编译不过,而get_indicator函数在编译过程中是不会运行的,所以列表为空值。

--  作者:yukizzc
--  发布时间:2020/7/23 10:11:02
--  

电话我 021-20339087