pandas数据可视化(三)之时间重采样

11 篇文章 0 订阅
10 篇文章 0 订阅

pandas数据可视化(三)之时间重采样

python+pandas生成指定日期和重采样 - CSDN博客  https://blog.csdn.net/LY_ysys629/article/details/73823803

Pandas中resample方法详解 - CSDN博客  https://blog.csdn.net/wangshuang1631/article/details/52314944

——————————————————————————————————————————————————

时间系列转换:

        c=pd.Series(np.random.rand(5), index=(pd.date_range('20180130', periods=5, freq='D')))  #创建时间戳系列
        d=c.to_period('M')                   #
        print( c, type(c), c.index)
        print( d, type(d), d.index)
2018-01-30    0.424927
2018-01-31    0.522582
2018-02-01    0.889830
2018-02-02    0.130641
2018-02-03    0.222065
Freq: D, dtype: float64 <class 'pandas.core.series.Series'> DatetimeIndex(['2018-01-30', '2018-01-31', '2018-02-01', '2018-02-02', '2018-02-03'], dtype='datetime64[ns]', freq='D')
2018-01    0.424927
2018-01    0.522582
2018-02    0.889830
2018-02    0.130641
2018-02    0.222065
Freq: M, dtype: float64 <class 'pandas.core.series.Series'> PeriodIndex(['2018-01', '2018-01', '2018-02', '2018-02', '2018-02'], dtype='period[M]', freq='M')


——————————————————————————————————————————————————

groupby按组汇总求和(sum)或平均数(mean)

     c=pd.Series(np.random.rand(5), index=(pd.date_range('20180130', periods=5, freq='D')))
        d=c.to_period('M')  转成月期间
        e=d.groupby(level=0).mean()   #或e=d.groupby(level=0).sum()
输出以下结果:
        2018-01    0.407399
        2018-02    0.826991

————————————————————————————————————————————————

时间再转换:

时间戳系列转成时期系列'M',再由时期系列转成时间戳系列,后面的时间戳的“日”,不再是原来的时间戳的日数。

        c=pd.Series(np.random.rand(3), index=(pd.date_range('20180130', periods=3, freq='D')))
        d=c.to_period('M')
        f=d.to_timestamp(how='start')   #或者how='end'

——————————————————————————————————————————————

时间重采样:时间戳系列索引,分钟转为5分钟

        c=pd.Series(np.random.randint(0, 50, 11), index=(pd.date_range('2018-01-30 9:30', periods=11, freq='T')))
        d=c.resample('5min', how='sum', label='right')#label='right',表示右侧时间(5分钟时间段的后端),默认为始端时间
        e=c.resample('5min', how='ohlc')
        print(c, d, e)

输出结果:

the new syntax is .resample(...).ohlc()
  e=c.resample('5min', how='ohlc')
2018-01-30 09:30:00    27
2018-01-30 09:31:00     5
2018-01-30 09:32:00    36
2018-01-30 09:33:00    49
2018-01-30 09:34:00    23
2018-01-30 09:35:00    44
2018-01-30 09:36:00    46
2018-01-30 09:37:00     7
2018-01-30 09:38:00     4
2018-01-30 09:39:00    39
2018-01-30 09:40:00    40
Freq: T, dtype: int32 
2018-01-30 09:35:00    140
2018-01-30 09:40:00    140     #以5分钟时期的右侧时间9:35作为取样时间点,默认为本段时间期间的左侧9:30作为取样时间点
2018-01-30 09:45:00     40
Freq: 5T, dtype: int32 open  high  low  close
2018-01-30 09:30:00    27    49    5     23    #c.resample('5min', how='ohlc'),以5分钟内第1个价为开盘价,最后1个价为闭盘价。
2018-01-30 09:35:00    44    46    4     39
2018-01-30 09:40:00    40    40   40     40

————————————————————————————————————

时间重采样:

        c=pd.Series(np.random.randint(0, 50, 10), index=pd.date_range('2018-1-24', periods=10, freq='D'))
        d=c.groupby(lambda x:x.month).sum()
        e=c.groupby(c.index.to_period('M')).sum()
        print(c)
        print(d)
        print(e)

输出结果:print(c)

2018-01-24    46
2018-01-25    38
2018-01-26    35
2018-01-27    13
2018-01-28    43
2018-01-29    27
2018-01-30    30
2018-01-31    25
2018-02-01    31
2018-02-02    17
Freq: D, dtype: int32
print(d)
1    257
2     48
dtype: int32
print(e)
2018-01    257
2018-02     48
Freq: M, dtype: int32

        c=pd.Series(np.random.randint(0, 50, 2), index=pd.date_range('20180401', periods=2, freq='W-FRI'))
        d=c.resample('D', fill_method='ffill', limit=2)   #
        e=c.resample('W-MON', fill_method='ffill')
        print(c)
        print(d)
        print(e)


输出结果:

2018-04-06    34
2018-04-13     1
Freq: W-FRI, dtype: int32
2018-04-06    34.0
2018-04-07    34.0
2018-04-08    34.0
2018-04-09     NaN
2018-04-10     NaN
2018-04-11     NaN
2018-04-12     NaN
2018-04-13     1.0
Freq: D, dtype: float64
2018-04-09    34
2018-04-16     1
Freq: W-MON, dtype: int32
——————————————————————————————————————————————————
时间重采样:采样为年度、季度
        c=pd.DataFrame(np.random.randint(2, 30, (15, 4)), index=pd.date_range('2018-3-2', periods=15, freq='M'), columns=list('abcd'))
        d=c.resample('A-DEC', how='sum')   #
        e=c.resample('A-MAR', how='sum')
        f=c.resample('Q-DEC', how='sum')
        print(c)
        print(d)
        print(e)
        print(f)


输出结果:print(c)

             a   b   c   d
2018-03-31  20  29   4  25
2018-04-30   3   5   6  18
2018-05-31  12   4   8  12
2018-06-30   3   4  18  20
2018-07-31  25  14   4  27
2018-08-31  22   9  24  21
2018-09-30   8  26   2  28
2018-10-31   9   3   4  27
2018-11-30  23   6   6  27
2018-12-31  25  29  18   6
2019-01-31  21  19  20  11
2019-02-28   5   7  28  28
2019-03-31  16  19  21  17
2019-04-30  16  19   9   9
2019-05-31  16   7  28  15
输出结果:print(d)
              a    b    c    d
2018-12-31  150  129   94  211
2019-12-31   74   71  106   80
输出结果:print(e)
              a    b    c    d
2018-03-31   20   29    4   25
2019-03-31  172  145  159  242
2020-03-31   32   26   37   24
输出结果:print(f)
             a   b   c   d
2018-03-31  20  29   4  25
2018-06-30  18  13  32  50
2018-09-30  55  49  30  76
2018-12-31  57  38  28  60
2019-03-31  42  45  69  56
2019-06-30  32  26  37  24

——————————————————————————————————————————

股票数据周期转换:

        c=pd.read_csv('601656', index_col='Date', parse_dates=True)
        d=c['adj_close'].resample('W-FRI', how='ohlc')   #收盘价按周重采样
        d['vol']=c['vol'].resample('W-FRI', how='sum')   #向d追加交易量周采样列





  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值