numpy.ndarray的数据添加元素并转成pandas

准备利用rqalpha做一个诊股系统,当然先要将funcat插件调试好,然后即可将同花顺上的易语言搬到rqalpha中使用了,根据一定规则将各股票进行打分,看起来可以勉强使用了。只有一点,得到的数据不够新,一般总是滞后一天,需要将爬取的实时数据保存到系统中,然后利用系统进行诊股。
首先需要考虑如何在ndarray中添加元素,以下为方法,最后将之保存到pandas中,再保存回bcolz数据中

1 单维数组添加

dtype = np.dtype([('date', 'uint32')])
result = np.empty(shape=(3,), dtype=dtype)
result = np.array([20180405,20180406,20180407], dtype=dtype)
result = np.array([20180405,20180406,20180407], dtype=dtype)
result = np.append(result, np.array([20180409], dtype=dtype))
print(result)
print(result['date'])

2 多维数组添加

import numpy as np
dtype = np.dtype([('date', 'uint32'), ('close', 'uint32')])
result = np.empty(shape=(3,), dtype=dtype)
result['date'] = np.array([20180405,20180406,20180407], dtype=np.uint32)
result['close'] = np.array([32, 50, 45], dtype=np.uint32)
print(result)
result = np.append(result, np.array([(20180409, 50)], dtype=dtype))
print(result)

3 字符串相关

import numpy as np

dtype = np.dtype([('date', 'uint32'), ('close', np.uint32), ('name', np.object)])
result = np.empty(shape=(0,), dtype=dtype)
result = np.append(result, np.array([(20180409, 50, "abcdef")], dtype=dtype))
print(result)

4 转成pandas

import pandas as pd
arr = pd.DataFrame(result)
print(arr)

5 多维数组添加

2 的添加方式对于数据量很大的情况下明显速度会很慢,可以采用先预分配空间,再修改数据的方式:

import numpy as np
dtype = np.dtype([('date', 'uint32'), ('close', 'uint32')])
result = np.empty(shape=(2,), dtype=dtype)
result[0] = np.array([(20200401, 33)], dtype=dtype)
print(result)
result[1] = np.array([(20200501, 99)], dtype=dtype)
print(result)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

永远的麦田

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值