python之pandas库的时间对象处理datetime/dateutil

python之pandas库的时间对象处理

目录

1.时间序列类型
时间戳:特定时刻
固定时期:固定一个时刻
时间间隔:起始时间-结束时间

2.python标准库处理时间对象datetime
灵活处理时间对象:dateutil
dateutil.parser.parse()
成组处理时间对象:pandas
pd.to_datetime()
生成时间对象数组:
pd.date_range()

代码实现:

import pandas as pd 
import numpy as np
import datetime
#字符串转换为datetime格式
a = datetime.datetime.strptime('2010-01-01','%Y-%m-%d') #第一个datetime是模块,第二个datetime是类
#但是上面这种方法需要前后格式匹配,因此采用dateutil库
import dateutil
b = dateutil.parser.parse('2010-01-01')
c = dateutil.parser.parse('01/01/2010')
d = dateutil.parser.parse('2010-JAN-01')
#批量转化为datetime
e = pd.to_datetime(['2010-01-01','2020/01/20','2030-Feb-01'])



print('a=',a,'\n')
print('b=',b,'\n')
print('c=',c,'\n')
print('d=',d,'\n')
print('e=',e,'\n')

a= 2010-01-01 00:00:00 

b= 2010-01-01 00:00:00 

c= 2010-01-01 00:00:00 

d= 2010-01-01 00:00:00 

e= DatetimeIndex(['2010-01-01', '2020-01-20', '2030-02-01'], dtype='datetime64[ns]', freq=None) 
a = pd.date_range('2010-01-01','2010-02-01')
b = pd.date_range('2010-01-01',periods=30) #period指输出日期个数
c = pd.date_range('2010-01-01',periods=30,freq='H') 
#freq设置:H-小时,W-周,B-工作日,SM-半月,T-分,S-秒,A-年
#也可以用1h20min等等
d = pd.date_range('2010-01-01',periods=30,freq='W-SUN') #W-SUN指按周输出,输出日期为周日
e = pd.date_range('2010-01-01',periods=30,freq='B') #B指按工作日输出
#此时的元素为timestamp类型
#转化为datetime类型
a1 = a[0].to_pydatetime()
#datetime转化为字符串类型
a2 = str(a1)

print('a=',a,'\n')
print('b=',b,'\n')
print('c=',c,'\n')
print('d=',d,'\n')
print('e=',e,'\n')
print('a1=',a1,'\n')
print('a2=',a2,'\n')
a= DatetimeIndex(['2010-01-01', '2010-01-02', '2010-01-03', '2010-01-04',
               '2010-01-05', '2010-01-06', '2010-01-07', '2010-01-08',
               '2010-01-09', '2010-01-10', '2010-01-11', '2010-01-12',
               '2010-01-13', '2010-01-14', '2010-01-15', '2010-01-16',
               '2010-01-17', '2010-01-18', '2010-01-19', '2010-01-20',
               '2010-01-21', '2010-01-22', '2010-01-23', '2010-01-24',
               '2010-01-25', '2010-01-26', '2010-01-27', '2010-01-28',
               '2010-01-29', '2010-01-30', '2010-01-31', '2010-02-01'],
              dtype='datetime64[ns]', freq='D') 

b= DatetimeIndex(['2010-01-01', '2010-01-02', '2010-01-03', '2010-01-04',
               '2010-01-05', '2010-01-06', '2010-01-07', '2010-01-08',
               '2010-01-09', '2010-01-10', '2010-01-11', '2010-01-12',
               '2010-01-13', '2010-01-14', '2010-01-15', '2010-01-16',
               '2010-01-17', '2010-01-18', '2010-01-19', '2010-01-20',
               '2010-01-21', '2010-01-22', '2010-01-23', '2010-01-24',
               '2010-01-25', '2010-01-26', '2010-01-27', '2010-01-28',
               '2010-01-29', '2010-01-30'],
              dtype='datetime64[ns]', freq='D') 

c= DatetimeIndex(['2010-01-01 00:00:00', '2010-01-01 01:00:00',
               '2010-01-01 02:00:00', '2010-01-01 03:00:00',
               '2010-01-01 04:00:00', '2010-01-01 05:00:00',
               '2010-01-01 06:00:00', '2010-01-01 07:00:00',
               '2010-01-01 08:00:00', '2010-01-01 09:00:00',
               '2010-01-01 10:00:00', '2010-01-01 11:00:00',
               '2010-01-01 12:00:00', '2010-01-01 13:00:00',
               '2010-01-01 14:00:00', '2010-01-01 15:00:00',
               '2010-01-01 16:00:00', '2010-01-01 17:00:00',
               '2010-01-01 18:00:00', '2010-01-01 19:00:00',
               '2010-01-01 20:00:00', '2010-01-01 21:00:00',
               '2010-01-01 22:00:00', '2010-01-01 23:00:00',
               '2010-01-02 00:00:00', '2010-01-02 01:00:00',
               '2010-01-02 02:00:00', '2010-01-02 03:00:00',
               '2010-01-02 04:00:00', '2010-01-02 05:00:00'],
              dtype='datetime64[ns]', freq='H') 

d= DatetimeIndex(['2010-01-03', '2010-01-10', '2010-01-17', '2010-01-24',
               '2010-01-31', '2010-02-07', '2010-02-14', '2010-02-21',
               '2010-02-28', '2010-03-07', '2010-03-14', '2010-03-21',
               '2010-03-28', '2010-04-04', '2010-04-11', '2010-04-18',
               '2010-04-25', '2010-05-02', '2010-05-09', '2010-05-16',
               '2010-05-23', '2010-05-30', '2010-06-06', '2010-06-13',
               '2010-06-20', '2010-06-27', '2010-07-04', '2010-07-11',
               '2010-07-18', '2010-07-25'],
              dtype='datetime64[ns]', freq='W-SUN') 

e= DatetimeIndex(['2010-01-01', '2010-01-04', '2010-01-05', '2010-01-06',
               '2010-01-07', '2010-01-08', '2010-01-11', '2010-01-12',
               '2010-01-13', '2010-01-14', '2010-01-15', '2010-01-18',
               '2010-01-19', '2010-01-20', '2010-01-21', '2010-01-22',
               '2010-01-25', '2010-01-26', '2010-01-27', '2010-01-28',
               '2010-01-29', '2010-02-01', '2010-02-02', '2010-02-03',
               '2010-02-04', '2010-02-05', '2010-02-08', '2010-02-09',
               '2010-02-10', '2010-02-11'],
              dtype='datetime64[ns]', freq='B') 

a1= 2010-01-01 00:00:00 

a2= 2010-01-01 00:00:00 

你可能感兴趣的:(python基础知识,python,pandas,开发语言)