pandas 6 时间

备注创建方法
Timestamp 时刻数据 to_datetime,Timestamp
DatetimeIndex Timestamp的索引 to_datetime,date_range,DatetimeIndex
Period 时期数据 Period
PeriodIndex Period period_range,PeriodIndex
print(pd.Timestamp('2018-05-21 00:00:00')) # pd.Timestamp(2018, 5, 21) 时间戳
print(pd.Period("2018-01", freq="D"))
pd.to_datetime(pd.Series(["Jul 31, 2018", "2018-05-10", None])) # 通过 to_datetime 能快速将字符串转换为时间戳
pd.to_datetime([1349720105, 1349806505, 1349892905], unit="s")
# date_range 默认使用的频率是 日历日,而 bdate_range 默认使用的频率是 营业日。
pd.date_range("2018-6-26", periods=8)
pd.date_range("2018-6-26", periods=8, freq="W")
pd.date_range("2018-6-26", periods=8)
##### 当将时间作为index时,方便切片,也方便按组索引,如所以2018年的数据
rng = pd.date_range("2018-6-10", periods=5, freq="W")
ts = pd.Series(range(len(rng)), index=rng)
print(ts["2018-07-08": "2018-07-22"]) # 当将时间作为index时,方便切片
print(ts["2018-07"]) # 方便按组索引,如所以2018年的数据
ts.shift(2) #移动值
print(ts.resample("1M").sum()) # 求出每个月的数值之和
ts.shift(2, freq=Day()) #移动时间索引
ts.asfreq(Day(), method="pad") #时间频率转换,将频率由周转为了天
# 对时间做偏移
from pandas.tseries.offsets import *
d = pd.Timestamp("2018-06-25")
d + DateOffset(weeks=2, days=5)
'''
我们可以通过 Timestamp 或 DateTimeIndex 访问一些时间/日期的属性。这里列举一些常见的,想要查看所有的属性见官方链接:Time/Date Components(http://pandas.pydata.org/pandas-docs/stable/timeseries.html#time-date-components)
'''
原文地址:https://www.cnblogs.com/testzcy/p/12077830.html