zoukankan      html  css  js  c++  java
  • Pandas的时间序列处理

    创建

    from datetime import datetime
    import pandas as pd
    import numpy as np
    
    # 指定index为datetime的list
    date_list = [datetime(2017, 2, 18), datetime(2017, 2, 19), 
                 datetime(2017, 2, 25), datetime(2017, 2, 26), 
                 datetime(2017, 3, 4), datetime(2017, 3, 5)]
    time_s = pd.Series(np.random.randn(6), index=date_list)
    print(time_s)
    print(type(time_s.index))
    
    2017-02-18   -0.543551
    2017-02-19   -0.759103
    2017-02-25    0.058956
    2017-02-26    0.275448
    2017-03-04   -0.957346
    2017-03-05   -1.143108
    dtype: float64
    <class 'pandas.tseries.index.DatetimeIndex'>
    <class 'pandas.core.series.Series'>
    

    索引

    过滤

    生成日期范围

    频率与偏移量

    print(pd.date_range('2017/02/18', '2017/03/18', freq='2D'))
    
    # 偏移量通过加法连接
    sum_offset = pd.tseries.offsets.Week(2) + pd.tseries.offsets.Hour(12)
    print(sum_offset)
    
    print(pd.date_range('2017/02/18', '2017/03/18', freq=sum_offset))
    
    
    DatetimeIndex(['2017-02-18', '2017-02-20', '2017-02-22', '2017-02-24',
                   '2017-02-26', '2017-02-28', '2017-03-02', '2017-03-04',
                   '2017-03-06', '2017-03-08', '2017-03-10', '2017-03-12',
                   '2017-03-14', '2017-03-16', '2017-03-18'],
                  dtype='datetime64[ns]', freq='2D')
                  
    14 days 12:00:00
    DatetimeIndex(['2017-02-18 00:00:00', '2017-03-04 12:00:00'], dtype='datetime64[ns]', freq='348H')
    
    

    移动数据


    时间周期计算

  • 相关阅读:
    1442. Count Triplets That Can Form Two Arrays of Equal XOR
    1441. Build an Array With Stack Operations
    312. Burst Balloons
    367. Valid Perfect Square
    307. Range Sum Query
    1232. Check If It Is a Straight Line
    993. Cousins in Binary Tree
    1436. Destination City
    476. Number Complement
    383. Ransom Note
  • 原文地址:https://www.cnblogs.com/xuehaozhe/p/Pandas-de-shi-jian-xu-lie-chu-li.html
Copyright © 2011-2022 走看看