时间序列主要有以下三种:
时间戳(timstamp):特定的时刻
固定时期(period):比如2007年全年
时间间隔(interval):起始时间戳–结束时间戳
Python时间模块回顾:
datetime模块:https://blog.csdn.net/weixin_44595372/article/details/87995896
time模块
https://blog.csdn.net/weixin_44595372/article/details/87992590
时间序列的主要方法
1. pandas.to_datetime(): 将对象转换为datetime对象:
语法:
pandas.to_datetime(arg, dayfirst=False, yearfirst=False, utc=None, box=True, format=None, exact=True, unit=None, infer_datetime_format=False, origin=‘unix’, cache=False)pandas.to_datetime(arg, errors=‘raise’, dayfirst=False, yearfirst=False, utc=None, box=True, format=None, exact=True, unit=None, infer_datetime_format=False, origin=‘unix’, cache=False)
几个主要参数:
args:可以是整数,浮点数,datetime,列表,元祖,一维数组,series,dataframe/字典
dayfirst: 如传入的arg参数是str或者list-like数据,则认为原参数的顺序为 d/m/y, 比如10/11/12 解析为2012-11-10
yearfirst:如传入的arg参数是str或者list-like数据,则认为原参数的顺序为 y/m/d, 比如:10/11/12 解析为2010-11-12
format: 自定义输出格式,datetime格式定义见附录1
#示例:传入一个整数元祖:
import pandas as pd
dt=pd.to_datetime((1,2,3),format='%Y-%m-%d')
print(dt)
#输出
DatetimeIndex(['1970-01-01 00:00:00.000000001',
'1970-01-01 00:00:00.000000002',
'1970-01-01 00:00:00.000000003'],
dtype='datetime64[ns]', freq=None)
2. pandas.date_range(start=None, end=None, periods=None, freq=None)
生成均匀分布的时间序列
参数:
start:字符串/datetime 起始值
end:字符串/datetime 结束值
periods:生成个数
freq:偏移量,str, 默认按天计算,为’D’ ,如果是5天一次则为‘5D’, 格式定义如附录2