时间序列数据分析--Time Series--python日期和时间处理及操作

时间序列分类

  • 时间戳(timestamp),特定的时刻
  • 固定周期(period),某月或某年
  • 时间间隔(interval),由起始时间戳和结束时间戳表示

python日期和时间处理及操作 

1.datetime模块

datetime -> str

  • (1)   str(datetime_obj)
  • (2)   datetime.strftime()
  • from datetime import datetime
    from datetime import date
    
    now = datetime.now()
    print(now)
    
    diff = datetime(2017, 3, 4, 17) - datetime(2017, 2, 18, 15)
    print(type(diff))
    print(diff)
    print('经历了{}天, {}秒。'.format(diff.days, diff.seconds))
    answer:
    
    14 days, 2:00:00
    经历了14天, 7200秒。
    
    datetime -> str
    
    # str()
    dt_obj = datetime(2017, 3, 4)
    str_obj = str(dt_obj)
    print(type(str_obj))
    print(str_obj)
    answer:
    
    2017-03-04 00:00:00
    
    # datetime.strftime()
    str_obj2 = dt_obj.strftime('%d-%m-%Y')
    print(type(str_obj2))
    

     

str -> datetime

  • (1)datetime.strptime() 需要指定时间表示的格式
  • (2)dateutil.parser.parser() 可以解析大部分时间表示形式
  • (3)pd.to_datetime()可以处理缺失值和空字符串
  • str -> datetime
    
    # strptime
    dt_str = '2017-02-18'
    dt_obj2 = datetime.strptime(dt_str, '%Y-%m-%d')
    print(type(dt_obj2))
    print(dt_obj2)
    
    # dateutil.parser.parse
    from dateutil.parser import parse
    dt_str2 = '2017/02/18'
    dt_obj3 = parse(dt_str2)
    print(type(dt_obj3))
    print(dt_obj3)
    
    # pd.to_datetime
    import pandas as pd
    s_obj = pd.Series(['2017/02/18', '2017/02/19', '2017-02-25', '2017-02-26'], name='course_time')
    print(s_obj)
    s_obj2 = pd.to_datetime(s_obj)
    print(s_obj2)
    
    # 处理缺失值
    s_obj3 = pd.Series(['2017/02/18', '2017/02/19', '2017-02-25', '2017-02-26'] + [None], 
                       name='course_time')
    print(s_obj3)
    s_obj4 = pd.to_datetime(s_obj3)
    print(s_obj4) # NAT-> Not a Time
    answer:
    0   2017-02-18
    1   2017-02-19
    2   2017-02-25
    3   2017-02-26
    4          NaT
    Name: course_time, dtype: datetime64[ns]

     

你可能感兴趣的:(python)