time类型
Timestamp类
- str to timestamp
pd.to_datetime(arg.format=’%Y%m%d%H%M%S’,errors)
pd.DatetimeIndex:和上面一样不过把时间设为索引
#Errors =raise 显示错误信息 coerce 遇到错误设为nan ignore转换能转换的值
- timestamp to str
timestamp.strftime(‘format’)
timedelta类
用于时间加减
pd.timedelta()
period类
periodindex.asfreq() #转换频率
df.to_period() #Convert DataFrame from DatetimeIndex to PeriodIndex.
根据时间来重采样
df.resample(freq, how=None, axis=0, fill_method=None, closed=None, label=None, convention='start', kind=None, loffset=None, limit=None, base=0)
#how='mean' 用于产生聚合值的函数名或数组函数,例如‘mean'、‘ohlc'、np.max等,默认是‘mean',其他常用的值由:‘first'、‘last'、‘median'、‘max'、‘min'
#axis=0 默认是纵轴,横轴设置axis=1
#fill_method = None 升采样时如何插值,比如‘ffill'、‘bfill'等
#closed = ‘right' 在降采样时,各时间段的哪一段是闭合的,‘right'或‘left',默认‘right'
#label= ‘right' 在降采样时,如何设置聚合值的标签,例如,9:30-9:35会被标记成9:30还是9:35,默认9:35
#loffset = None 面元标签的时间校正值,比如‘-1s'或Second(-1)用于将聚合标签调早1秒
#limit=None 在向前或向后填充时,允许填充的最大时期数
#kind = None 聚合到时期(‘period')或时间戳(‘timestamp'),默认聚合到时间序列的索引类型
#convention = None 当重采样时期时,将低频率转换到高频率所采用的约定(start或end)。默认‘end'
ps:这个函数得使用timestamp作为index
生成日期范围
pandas.date_range(start=None, end=None, periods=None, freq=None, tz=None, normalize=False, name=None, closed=None, **kwargs)
#基础时间序列
#D每天 B工作日每天 H min S ms U微米 M月底日期 BM工作日月底日期 MS日历日月初日期
日期移位
df.shift(number, freq='')
时间格式
类型 | 描述 |
---|---|
%Y | 4位年份 |
%y | 2位年份 |
%m | 两位月 |
%d | 两位日期号 |
%H | 24小时制 |
%I | 12小时制 |
%M | 两位分钟 |
%S | 秒 |
%W | 星期日期 日=0 |