# -*- coding: utf-8 -*-
"""
Created on Fri May 24 19:59:16 2019
@author: User
"""
import pandas as pd
import numpy as np
print("\n 按工作日生成的索引:")
date=pd.date_range('2019-03-13','2019-03-29',freq='B')
sample=pd.DataFrame({'open_p':np.random.randn(13),'close_p':np.random.randn(13)},
index=date)
print(sample)
print("\n 按日历日生成的索引:")
print(sample.asfreq(freq='D'))
print("\n sample.resample('12H').ffill() 按照半天频率进行上采样,指定插值方式:")
print(sample.resample('12H').ffill())
print("\n sample.resample('4D').ohlc() 按照4天频率进行采样(起点 最大 最小 终点数据):")
print(sample.resample('4D').ohlc())
print("\n sample.resample('4D').ohlc() 按照4天频率进行采样(起点 最大 最小 终点数据):")
print(sample.groupby(lambda x:x.week).mean())
print("\n sample[['open_p','close_p']].groupby(lambda x:x.month).mean() 提取股票月开收盘价:")
print(sample[['open_p','close_p']].groupby(lambda x:x.month).mean())
运行:
按工作日生成的索引:
open_p close_p
2019-03-13 1.237511 0.167589
2019-03-14 -2.011978 -0.575576
2019-03-15 0.548313 0.401648
2019-03-18 -0.981720 0.173023
2019-03-19 -0.532745 -0.413077
2019-03-20 0.898792 -0.029455
2019-03-21 -0.559489 -0.472909
2019-03-22 -0.899932 -0.304375
2019-03-25 -1.850232 0.436640
2019-03-26 0.306646 0.518828
2019-03-27 1.242612 -1.255874
2019-03-28 0.616150 1.920751
2019-03-29 0.043343 1.076985
按日历日生成的索引:
open_p close_p
2019-03-13 1.237511 0.167589
2019-03-14 -2.011978 -0.575576
2019-03-15 0.548313 0.401648
2019-03-16 NaN NaN
2019-03-17 NaN NaN
2019-03-18 -0.981720 0.173023
2019-03-19 -0.532745 -0.413077
2019-03-20 0.898792 -0.029455
2019-03-21 -0.559489 -0.472909
2019-03-22 -0.899932 -0.304375
2019-03-23 NaN NaN
2019-03-24 NaN NaN
2019-03-25 -1.850232 0.436640
2019-03-26 0.306646 0.518828
2019-03-27 1.242612 -1.255874
2019-03-28 0.616150 1.920751
2019-03-29 0.043343 1.076985
sample.resample('12H').ffill() 按照半天频率进行上采样,指定插值方式:
open_p close_p
2019-03-13 00:00:00 1.237511 0.167589
2019-03-13 12:00:00 1.237511 0.167589
2019-03-14 00:00:00 -2.011978 -0.575576
2019-03-14 12:00:00 -2.011978 -0.575576
2019-03-15 00:00:00 0.548313 0.401648
2019-03-15 12:00:00 0.548313 0.401648
2019-03-16 00:00:00 0.548313 0.401648
2019-03-16 12:00:00 0.548313 0.401648
2019-03-17 00:00:00 0.548313 0.401648
2019-03-17 12:00:00 0.548313 0.401648
2019-03-18 00:00:00 -0.981720 0.173023
2019-03-18 12:00:00 -0.981720 0.173023
2019-03-19 00:00:00 -0.532745 -0.413077
2019-03-19 12:00:00 -0.532745 -0.413077
2019-03-20 00:00:00 0.898792 -0.029455
2019-03-20 12:00:00 0.898792 -0.029455
2019-03-21 00:00:00 -0.559489 -0.472909
2019-03-21 12:00:00 -0.559489 -0.472909
2019-03-22 00:00:00 -0.899932 -0.304375
2019-03-22 12:00:00 -0.899932 -0.304375
2019-03-23 00:00:00 -0.899932 -0.304375
2019-03-23 12:00:00 -0.899932 -0.304375
2019-03-24 00:00:00 -0.899932 -0.304375
2019-03-24 12:00:00 -0.899932 -0.304375
2019-03-25 00:00:00 -1.850232 0.436640
2019-03-25 12:00:00 -1.850232 0.436640
2019-03-26 00:00:00 0.306646 0.518828
2019-03-26 12:00:00 0.306646 0.518828
2019-03-27 00:00:00 1.242612 -1.255874
2019-03-27 12:00:00 1.242612 -1.255874
2019-03-28 00:00:00 0.616150 1.920751
2019-03-28 12:00:00 0.616150 1.920751
2019-03-29 00:00:00 0.043343 1.076985
sample.resample('4D').ohlc() 按照4天频率进行采样(起点 最大 最小 终点数据):
open_p ... close_p
open high low ... high low close
2019-03-13 1.237511 1.237511 -2.011978 ... 0.401648 -0.575576 0.401648
2019-03-17 -0.981720 0.898792 -0.981720 ... 0.173023 -0.413077 -0.029455
2019-03-21 -0.559489 -0.559489 -0.899932 ... -0.304375 -0.472909 -0.304375
2019-03-25 -1.850232 1.242612 -1.850232 ... 1.920751 -1.255874 1.920751
2019-03-29 0.043343 0.043343 0.043343 ... 1.076985 1.076985 1.076985
[5 rows x 8 columns]
sample.resample('4D').ohlc() 按照4天频率进行采样(起点 最大 最小 终点数据):
open_p close_p
11 -0.075385 -0.002113
12 -0.415019 -0.209358
13 0.071704 0.539466
sample[['open_p','close_p']].groupby(lambda x:x.month).mean() 提取股票月开收盘价:
open_p close_p
3 -0.149441 0.126477