Python标准库包含用于日期和时间数据的数据类型,主要用到datetime、time、calendar模块。
datetime模块常使用datetime和timedelta两种实例方法
引入datetime模块
import datetime
生成datetime对象
start_date = datetime(2018,1,1)
print(type(start_date))
end_date = datetime(2018,12,31)
print(type(end_date))
delta_date = end_date - start_date
print(type(delta_date))
date_str = '2018-1-1'
date_strptime = datetime.strptime(date_str, '%Y-%m-%d')
print(type(date_strptime))
print(date_strptime)
date_str2 = '1-1-2018'
date_parse = parse(date_str2)
print(type(date_parse))
print(date_parse)
date_arr = ['1/1/2018','12/31/2018']
date_todatetime = pd.to_datetime(date_arr)
print(type(date_todatetime))
print(date_todatetime)
str
start_date = datetime(2018,1,1)
str_start_date = str(start_date)
print(type(str_start_date))
print(str_start_date)
start_date = datetime(2018,1,1)
strftime_start_date = start_date.strftime('%Y-%m-%d')
print(type(strftime_start_date))
print(strftime_start_date)
serial
ts = pd.Series(np.random.randn(6), index=date_list)
print(type(ts))
print(ts)
date_range()
dates = pd.date_range('2018-1-1', periods=5, freq='W-SAT')
print(dates)
print(pd.Series(np.random.randn(5), index=dates))
date_index = pd.date_range('2018/1/1', '2018/2/1')
print(date_index)
ts = pd.Series(np.random.randn(5), index=pd.date_range('20180101', periods=5, freq='W-SAT'))
print(ts)
print(ts.shift(1))
print(ts.shift(-1))
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。