首页 > 其他分享 >二.使用pandas.Resample函数转换日K为周K

二.使用pandas.Resample函数转换日K为周K

时间:2023-05-18 16:57:46浏览次数:40  
标签:11 10 函数 04 ... df Resample 05 pandas

from jqdatasdk import *
import pandas as pd
import time

auth('193340***', '****')  # 账号是申请时所填写的手机号;密码为聚宽官网登录密码

stocks = list(get_all_securities(['stock']).index)

# for stock_code in stocks:
#   print("正在股票所有数据:"+stock_code)
#  df = get_price(stock_code, end_date="2022-05-17 14:00:00", count=5, frequency="1d", fields=['open', 'close', 'high', 'low', 'volume', 'money'])
# print(df)
# time.sleep(3)


df = get_price("000001.XSHG", end_date="2021-05-20 14:00:00", count=20, frequency="1d", fields=['open', 'close', 'high', 'low', 'volume', 'money'])
#添加一列数据。星期几,0-6,周一为0

 

      2
df['weekday'] = df.index.weekday
print(df)
#转换周k
df_week = pd.DataFrame()
df_week['open'] = df['open'].resample('W').first()
df_week['close'] = df['close'].resample('W').last()
df_week['high'] = df['high'].resample('W').max()
df_week['low'] = df['low'].resample('W').min()
df_week['volume(sum)'] = df['volume'].resample('W').sum()#周汇总成交量
df_week['money(sum)'] = df['money'].resample('W').sum()#周汇总成交金额
print(df_week)



==============================================
日K转换为月K
==============================================
df = get_price("000001.XSHG", end_date="2021-05-20 14:00:00", start_date="2020-05-20 14:00:00",  frequency="1d", fields=['open', 'close', 'high', 'low', 'volume', 'money'])
#添加一列数据。星期几,0-6,周一为0
#2021-04-20 3467.15 3472.94 3494.30 ... 2.853001e+10 3.589828e+11 1
#2021-04-21 3456.30 3472.93 3481.25 ... 2.537779e+10 3.239141e+11 2
df['weekday'] = df.index.weekday
print(df)
#转换周-W-k,月M-K
df_week = pd.DataFrame()
df_week['open'] = df['open'].resample('M').first()
df_week['close'] = df['close'].resample('M').last()
df_week['high'] = df['high'].resample('M').max()
df_week['low'] = df['low'].resample('M').min()
df_week['volume(sum)'] = df['volume'].resample('M').sum()#月汇总成交量
df_week['money(sum)'] = df['money'].resample('M').sum()#月汇总成交金额
print(df_week)



open close high ... volume money weekday
2021-04-20 3467.15 3472.94 3494.30 ... 2.853001e+10 3.589828e+11 1
2021-04-21 3456.30 3472.93 3481.25 ... 2.537779e+10 3.239141e+11 2
2021-04-22 3482.83 3465.11 3485.36 ... 2.511145e+10 3.170810e+11 3
2021-04-23 3462.09 3474.17 3482.36 ... 2.493604e+10 3.230159e+11 4
2021-04-26 3484.11 3441.17 3497.12 ... 2.769708e+10 3.881617e+11 0
2021-04-27 3440.09 3442.61 3443.85 ... 2.530324e+10 3.270460e+11 1
2021-04-28 3432.16 3457.07 3457.07 ... 2.473811e+10 3.496692e+11 2
2021-04-29 3458.08 3474.90 3478.23 ... 2.766314e+10 3.716116e+11 3
2021-04-30 3468.30 3446.86 3469.09 ... 3.126604e+10 4.014423e+11 4
2021-05-06 3446.07 3441.28 3471.24 ... 3.104242e+10 4.005252e+11 3
2021-05-07 3446.41 3418.87 3457.89 ... 3.537854e+10 4.110788e+11 4
2021-05-10 3423.59 3427.99 3429.74 ... 3.741709e+10 3.997175e+11 0
2021-05-11 3406.60 3441.85 3448.10 ... 3.509341e+10 3.903263e+11 1
2021-05-12 3429.75 3462.75 3466.37 ... 3.114445e+10 3.438604e+11 2
2021-05-13 3432.14 3429.54 3448.02 ... 3.193253e+10 3.563769e+11 3
2021-05-14 3436.09 3490.38 3490.64 ... 3.369823e+10 4.111169e+11 4
2021-05-17 3490.41 3517.62 3530.51 ... 3.221360e+10 4.244768e+11 0
2021-05-18 3520.65 3529.01 3529.01 ... 2.713086e+10 3.360288e+11 1
2021-05-19 3521.11 3510.96 3521.11 ... 2.783211e+10 3.499732e+11 2
2021-05-20 3500.88 3506.94 3517.74 ... 3.260093e+10 3.903553e+11 3

open close high ... volume money weekday
2021-04-20 3467.15 3472.94 3494.30 ... 2.853001e+10 3.589828e+11 1
2021-04-21 3456.30 3472.93 3481.25 ... 2.537779e+10 3.239141e+11 2
2021-04-22 3482.83 3465.11 3485.36 ... 2.511145e+10 3.170810e+11 3
2021-04-23 3462.09 3474.17 3482.36 ... 2.493604e+10 3.230159e+11 4
2021-04-26 3484.11 3441.17 3497.12 ... 2.769708e+10 3.881617e+11 0
2021-04-27 3440.09 3442.61 3443.85 ... 2.530324e+10 3.270460e+11 1
2021-04-28 3432.16 3457.07 3457.07 ... 2.473811e+10 3.496692e+11 2
2021-04-29 3458.08 3474.90 3478.23 ... 2.766314e+10 3.716116e+11 3
2021-04-30 3468.30 3446.86 3469.09 ... 3.126604e+10 4.014423e+11 4
2021-05-06 3446.07 3441.28 3471.24 ... 3.104242e+10 4.005252e+11 3
2021-05-07 3446.41 3418.87 3457.89 ... 3.537854e+10 4.110788e+11 4
2021-05-10 3423.59 3427.99 3429.74 ... 3.741709e+10 3.997175e+11 0
2021-05-11 3406.60 3441.85 3448.10 ... 3.509341e+10 3.903263e+11 1
2021-05-12 3429.75 3462.75 3466.37 ... 3.114445e+10 3.438604e+11 2
2021-05-13 3432.14 3429.54 3448.02 ... 3.193253e+10 3.563769e+11 3
2021-05-14 3436.09 3490.38 3490.64 ... 3.369823e+10 4.111169e+11 4
2021-05-17 3490.41 3517.62 3530.51 ... 3.221360e+10 4.244768e+11 0
2021-05-18 3520.65 3529.01 3529.01 ... 2.713086e+10 3.360288e+11 1
2021-05-19 3521.11 3510.96 3521.11 ... 2.783211e+10 3.499732e+11 2
2021-05-20 3500.88 3506.94 3517.74 ... 3.260093e+10 3.903553e+11 3

  

  

标签:11,10,函数,04,...,df,Resample,05,pandas
From: https://www.cnblogs.com/gzhbk/p/17412437.html

相关文章

  • Mysql--函数
    时间,日期相关函数参考:https://www.cnblogs.com/Xinenhui/p/16348570.html 一、基本函数1.1count()统计数据表中包含的记录行的总数语法:SELECTCOUNT(*)FROMt1;SELECTCOUNT(字段名)FROMt1;用法:count(*)  统计表中的总行数,无论某列有值或者空值,包含nullcou......
  • 【pandas基础】--数据拆分与合并
    数据集拆分是将一个大型的数据集拆分为多个较小的数据集,可以让数据更加清晰易懂,也方便对单个数据集进行分析和处理。同时,分开的数据集也可以分别应用不同的数据分析方法进行处理,更加高效和专业。数据集合并则是将多个数据集合并成一个大的数据集,可以提供更全面的信息,也可以进行......
  • Python之函数
    python元组,字典,集合Python之函数函数的简介函数也是一个对象函数可以用来保存可执行的代码,并且可以在需要是,对这些语句进行多次调用创建函数语法:>def函数名(形参1,形参2......):>代码块注意:函数名必须符合标识符的命名规范(可以包含字母、数字、下划线但是不......
  • ftok()函数深度解析
    关于ftok函数,先不去了解它的作用,先来说说为什么要用它,共享内存,消息队列\信号量它们三个都是一个中间介质,来进行通信的.这种介质多的是。就是怎么区分出来,就像唯一一个身份证来区分人一样。你随便来一个就行,就是因为这。只要唯一就行,就想起来了文件的设备编号和节点,它是唯一的......
  • table常用用具函数 - list用法
     table_listext.luafunctionlist.reset(listTb,val)fori=1,#listTbdolistTb[i]=valendendfunctionlist.castItemToNum(listTb)fori=1,#listTbdolistTb[i]=tonumber(listTb[i])endreturnlistTbendfunctionlis......
  • table常用工具函数 - 表用法
     table_ext.lua---如果table不为空则新建functiontable.getEmptyTable(tb)ifnil==tbornil~=next(tb)thenreturn{}endreturntbendfunctiontable.isEmpty(tb)returnnil==tbornil==next(tb)endfunctiontable.swap(tb,k1......
  • 大数据Spark “蘑菇云”行动第93课:Hive中的内置函数、UDF、UDAF实战
     大数据Spark“蘑菇云”行动第93课:Hive中的内置函数、UDF、UDAF实战selectsum_all(age)from...hive>usedefault;showtables;select*fromemployeeforhaving;一:udf编码importorg.apache.hadoop.hive.ql.exec.UDF;importorg.apache.hadoop.io.Text;pub......
  • Linux多进程18-sigaction 信号捕捉函数
    #include<signal.h>intsigaction(intsignum,conststructsigaction*act,structsigaction*oldact);-功能:检查或者改变信号的处理,信号捕捉-参数:-signum:需要捕捉的信号的编号或者宏值(信号的名称)-act:捕捉到信......
  • Linux多进程05-exec函数族
    execl:执行文件函数#include<unistd.h>intexecl(constchar*pathname,constchar*arg,...); 执行参数path字符串所代表的文件路径参数:-path:需要指定的执行的文件的路径或者名称(推荐使用绝对路径)-arg:是执行可......
  • Linux多进程13-kill,raise,abort函数
    #include<sys/types.h>#include<signal.h>intkill(pid_tpid,intsig);-功能:给某个进程pid,发送某个信号sig-参数:-pid:>0:将信号发送给指定的进程=0:将信号发送给当前的进程组=-1:将信号发送给每一个......