zoukankan      html  css  js  c++  java
  • pandas生成时间列表

    pandas生成时间列表

    1.根据始末时间生成一个时间段

    pd.date_range(start, end, freq) freq**参数由英文(M D H Min 。。。)、英文数字结合。D表示一天,M表示一月如20D表示20天,5M表示5个月。

    #生成20171011-20171030 
    pd.date_range('20171011', '20171030',freq='5D') 
    DatetimeIndex(['2017-10-11', '2017-10-16', '2017-10-21', '2017-10-26'], dtype='datetime64[ns]', freq='5D')
    

    2.根据起始向后生成时间段 pd.date_range(日期字符串, periods=5, freq='T')

    periods :时间段长度,整数类型
    ​ freq: 时间单位。月日时分秒。M D H ...

    import pandas as pd 
    #20171231 12:50时间点开始,生成以月为间隔,长度为5的时间段 
    tm_rng = pd.date_range('20171231 12:50',periods=5,freq='M') 
     
    print(type(tm_rng)) 
    DatetimeIndex(['2017-12-31 12:50:00', '2018-01-31 12:50:00','2018-02-28 12:50:00', '2018-03-31 12:50:00',
     
    print(tm_rng)
    <class 'pandas.core.indexes.datetimes.DatetimeIndex'> 
    '2018-04-30 12:50:00'],dtype='datetime64[ns]', freq='M')
    

    3.根据给定时间点向前(向后)生成时间段

    ​ pd.bdate_range(end,periods,freq) 根据end时间点开始,以freq为单位,向前生成周期为period的时间序列

    ​ pd.bdate_range(start,periods,freq) 根据start时间点开始,以freq为单位,向后生成周期为period的时间序列

    #向前5天 
    print(pd.bdate_range(end='20180101',periods=5,freq='D'))
    DatetimeIndex(['2017-12-28', '2017-12-29', '2017-12-30', '2017-12-31','2018-01-01'],dtype='datetime64[ns]', freq='D')
    
    #向后5天 
    print(pd.bdate_range(start='20180101',periods=5,freq='D'))
    DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04','2018-01-05'],dtype='datetime64[ns]', freq='D')
    

    4.指定时间格式的时间列表

    传入的日期格式也可以修改,在pd.date_range函数中可以添加freq参数,默认是'D',表示天

    import pandas as pd
    def get_date_list(begin_date,end_date):
      date_list = [x.strftime('%Y-%m-%d') for x in list(pd.date_range(start=begin_date, end=end_date))]
      return date_list
    
    # 可以测试
    print(get_date_list('2018-06-01','2018-06-08'))
    
    #  ['2018-06-01', '2018-06-02', '2018-06-03', '2018-06-04', '2018-06-05', '2018-06-06', '2018-06-07', '2018-06-08']
    
  • 相关阅读:
    c#缓存技术(Dictionary)
    反射Reflection创建
    SQL Server手注之延时型盲注
    MySQL——事务
    MySQL——NULL值处理
    MySQL——连接的使用
    SQL server手注之报错注入
    SQL Serves手注之联合查询注入
    MySQL手注之ROOT权限处理
    MySQL——正则表达式
  • 原文地址:https://www.cnblogs.com/bbiu/p/11302052.html
Copyright © 2011-2022 走看看