weekdays.py
import numpy as np
from datetime import datetime
def datestr2num(s):
return datetime.strptime(s.decode('ascii'), "%d-%m-%Y").date().weekday() dates, open, high, low, close=np.loadtxt('data.csv', dtype=float, delimiter=',', usecols=(1, 3, 4, 5, 6), converters={1: datestr2num}, unpack=True)
close = close[:16]
dates = dates[:16]
# get first Monday
print (dates)
print ("open:",open)
print (np.where(dates == 0))
first_monday = np.ravel(np.where(dates == 0))[0]
print ("The first Monday index is", first_monday) print (np.where(dates == 4))
last_friday = np.ravel(np.where(dates == 4))[-1]
print ("The last Friday index is", last_friday) weeks_indices = np.arange(first_monday, last_friday + 1)
print ("Weeks indices initial", weeks_indices) weeks_indices = np.split(weeks_indices, 3)
print ("Weeks indices after split", weeks_indices) def summarize(a, o, h, l, c):
monday_open = o[a[0]]
week_high = np.max( np.take(h, a) )
week_low = np.min( np.take(l, a) )
friday_close = c[a[-1]]
return( 'APPL',monday_open, week_high, week_low, friday_close) weeksummary = np.apply_along_axis(summarize, 1, weeks_indices, open, high, low, close) print ("Week summary", weeksummary) ------------------------ OUT:

[4. 0. 1. 2. 3. 4. 0. 1. 2. 3. 4. 0. 1. 2. 3. 4.]
open: [344.17 335.8 341.3 344.45 343.8 343.61 347.89 353.68 355.19 357.39
354.75 356.79 359.19 360.8 357.1 358.21 342.05 338.77 344.02 345.29
351.21 355.47 349.96 357.2 360.07 361.11 354.91 354.69 349.69 345.4 ]
(array([ 1, 6, 11], dtype=int64),)
The first Monday index is 1
(array([ 0, 5, 10, 15], dtype=int64),)
The last Friday index is 15
Weeks indices initial [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
Weeks indices after split [array([1, 2, 3, 4, 5], dtype=int64), array([ 6, 7, 8, 9, 10], dtype=int64), array([11, 12, 13, 14, 15], dtype=int64)]
Week summary [['APPL' '335.8' '346.7' '334.3' '346.5']
['APPL' '347.8' '360.0' '347.6' '356.8']
['APPL' '356.7' '364.9' '349.5' '350.5']]

-------------------------------------------------------

如果将summarize函数中return的'APPL'去掉:
def summarize(a, o, h, l, c):
monday_open = o[a[0]]
week_high = np.max( np.take(h, a) )
week_low = np.min( np.take(l, a) )
friday_close = c[a[-1]]
return( monday_open, week_high, week_low, friday_close)
--------------------------------------------------------------- out:

Week summary [[335.8 346.7 334.3 346.5 ]
[347.89 360. 347.64 356.85]
[356.79 364.9 349.52 350.56]]

-------------------------------------------

小数点的精度发生了变化

备注:data.csv

AAPL,28-01-2011, ,344.17,344.4,333.53,336.1,21144800
AAPL,31-01-2011, ,335.8,340.04,334.3,339.32,13473000
AAPL,01-02-2011, ,341.3,345.65,340.98,345.03,15236800
AAPL,02-02-2011, ,344.45,345.25,343.55,344.32,9242600
AAPL,03-02-2011, ,343.8,344.24,338.55,343.44,14064100
AAPL,04-02-2011, ,343.61,346.7,343.51,346.5,11494200
AAPL,07-02-2011, ,347.89,353.25,347.64,351.88,17322100
AAPL,08-02-2011, ,353.68,355.52,352.15,355.2,13608500
AAPL,09-02-2011, ,355.19,359,354.87,358.16,17240800
AAPL,10-02-2011, ,357.39,360,348,354.54,33162400
AAPL,11-02-2011, ,354.75,357.8,353.54,356.85,13127500
AAPL,14-02-2011, ,356.79,359.48,356.71,359.18,11086200
AAPL,15-02-2011, ,359.19,359.97,357.55,359.9,10149000
AAPL,16-02-2011, ,360.8,364.9,360.5,363.13,17184100
AAPL,17-02-2011, ,357.1,360.27,356.52,358.3,18949000
AAPL,18-02-2011, ,358.21,359.5,349.52,350.56,29144500
AAPL,22-02-2011, ,342.05,345.4,337.72,338.61,31162200
AAPL,23-02-2011, ,338.77,344.64,338.61,342.62,23994700
AAPL,24-02-2011, ,344.02,345.15,338.37,342.88,17853500
AAPL,25-02-2011, ,345.29,348.43,344.8,348.16,13572000
AAPL,28-02-2011, ,351.21,355.05,351.12,353.21,14395400
AAPL,01-03-2011, ,355.47,355.72,347.68,349.31,16290300
AAPL,02-03-2011, ,349.96,354.35,348.4,352.12,21521000
AAPL,03-03-2011, ,357.2,359.79,355.92,359.56,17885200
AAPL,04-03-2011, ,360.07,360.29,357.75,360,16188000
AAPL,07-03-2011, ,361.11,361.67,351.31,355.36,19504300
AAPL,08-03-2011, ,354.91,357.4,352.25,355.76,12718000
AAPL,09-03-2011, ,354.69,354.76,350.6,352.47,16192700
AAPL,10-03-2011, ,349.69,349.77,344.9,346.67,18138800
AAPL,11-03-2011, ,345.4,352.32,345,351.99,16824200

												

关于numpy中的函数return中加入字符串类型数据后,小数点精度变化的更多相关文章

  1. java基础课程笔记 static 主函数 静态工具类 classpath java文档注释 静态代码块 对象初始化过程 设计模式 继承 子父类中的函数 继承中的构造函数 对象转型 多态 封装 抽象类 final 接口 包 jar包

    Static那些事儿 Static关键字 被static修饰的变量成为静态变量(类变量) 作用:是一个修饰符,用于修饰成员(成员变量,成员方法) 1.被static修饰后的成员变量只有一份 2.当成员 ...

  2. MySql中concat函数的用法(链接字符串)

    MySQL中concat函数使用方法:CONCAT(str1,str2,…) 返回结果为连接参数产生的字符串.如有任何一个参数为NULL ,则返回值为 NULL. 注意:如果所有参数均为非二进制字符串 ...

  3. Ajax中onreadystatechange函数不执行,是因为放在open()后

    今天动手写Ajax时遇到的问题:按照下面的顺序来写Ajax,功能是alert出txt文档的内容,在Chrome下可以执行onreadystatechange中的事件,在IE11及以下.FF就不能执行o ...

  4. 简单说明一下JS中的函数声明存在的“先使用,后定义”

    首先看一段JS代码,其中使用了两种方式声明了两个函数,分别在不同的地方调用两个函数: <script> 'use strict'; // 输出hello函数 console.log(hel ...

  5. 关于db2中listagg函数开发中的体验

    一.首先解释一下可能会查询的基础问题: 1.1db2 “with ur”是什么意思: 在DB2中,共有四种隔离级:RS,RR,CS,UR.以下对四种隔离级进行一些描述,同时附上个人做试验的结果.隔离级 ...

  6. [Python]Python Class 中的 函数定义中的 self

    In [80]: class MyClass001: ....: def selfDemo(self): ....: print 'My Demo' ....: In [81]: p = MyClas ...

  7. shell中的函数 shell中的数组 告警系统需求分析

     

  8. mysql中的字符串类型数据索引优化

    摘自 "高性能mysql" 对于一些字符串类型较长的字段搜索时, 可以参考如下方法

  9. MYSQL中的数值型数据类型与字符串类型

    /* 数值型数据类型主要用来存储数字,包含的类型有: TINYINT.SMALLINT.MEDIUMINT. INT(INTEGER). BIGINT TINGINT占1个字节,SMALLINT占2个 ...

随机推荐

  1. android studio 模拟器无法联网的解决方法

    本人环境 win10 64位+ android studio  自带的模拟器 1.首先把模拟器打开,然后需要把SDK文件夹下的platform-tools添加到系统变量中(此目录一定要不要弄错). 2 ...

  2. Netty从入门到精通到放弃

    有时间想记录一下关于Netty的知识. 第一课:传统IO的特点. package com.example.aimei.controller; import java.io.InputStream; i ...

  3. take it easy

    昨天魏辉在群里说,觉得自己以前很多事情做的不对,以前是个傻瓜,我说我也是这么觉得的.为什么这么说呢?因为我真的觉得以前的自己就是个SB.人的成长可能就是这样的吧,小时候的一些事情,或者是原生家庭的环境 ...

  4. Unity 自定义导入时切割Sprite

    using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; us ...

  5. 在树莓派3B、Ubuntu 18.04关闭板载Wifi、蓝牙

    树莓派没有传统的BIOS设置界面,文件/boot/firmware/config.txt就相当一个BIOS设置.这里是config.txt的详细文档:https://github.com/raspbe ...

  6. GIS案例学习笔记-三维生成和可视化表达

    GIS案例学习笔记-三维生成和可视化表达 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:针对栅格或者矢量数值型数据,进行三维可视化表达 操作时间:15分钟 案 ...

  7. jQuery入门学习

    一.jQuery的介绍 1.jQuery是一种轻量级的.兼容多浏览器的JavaScript库. 2.jQuery使用户能够方便处理HTML Document Events 实现动画效果.方便的进行Aj ...

  8. jQuery之遍历索引相关方法

    遍历索引相关方法: .each(),补充.children() .index() 1 .each() 2..children() .index()

  9. Tensorflow图像处理以及数据读取

    关于tensoflow的图像的处理,看到了一篇文章,个人觉得不错.https://blog.csdn.net/weiwei9363/article/details/79917942

  10. C# File API

    [C# File API] 1.System.IO.File Provides static methods for the creation, copying, deletion, moving, ...