numpy中dtype

简单说明dtype使用方法:

Rule为规则类,其中有3个字段,1为类型,2为计算规则,3为保留位数

如 :'close': Rule(float64, 1 / 10000.0, 2),

表示收盘价,Rule有三个字段,首为浮点类型,次为计算规则,末为保留小数位数

Converter类则是Rule字典集合,这里表示的为open,close等为key的集合

 
 
# /usr/bin/python3
# -*- encoding: utf-8 -*-
from collections import namedtuple
import numpy as np
 
 
float64 = np.dtype('float64')
Rule = namedtuple('Rule', ['dtype', 'multiplier', 'round'])
class Converter(object):
    def __init__(self, rules):
        self._rules = rules
 
 
    def convert(self, name, data):
        try:
            r = self._rules[name]
        except KeyError:
            return data
 
 
        result = data * r.multiplier
        if r.round:
            result = np.round(result, r.round)
 
 
        return result
 
 
    def field_type(self, name):
        try:
            return self._rules[name].dtype
        except KeyError:
            return self._rules['open'].dtype
              
if __name__ == '__main__':
    
    StockBarConverter = Converter({
        'open': Rule(float64, 1 / 10000.0, 4),
        'close': Rule(float64, 1 / 10000.0, 2),
        'high': Rule(float64, 1 / 10000.0, 2),
        'low': Rule(float64, 1 / 10000.0, 2),
        'limit_up': Rule(float64, 1/10000.0, 2),
        'limit_down': Rule(float64, 1/10000.0, 2),
        'volume': Rule(float64, 1, 0),
    })
    _converter = StockBarConverter
    data = np.array([1001, 2103])
    result = _converter.convert('open', data)
    open_type = _converter.field_type("open")
    print("open_type: ", open_type) #open_type:  float64

转载于:https://www.cnblogs.com/luhouxiang/p/7401859.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值