import time
time.time() #秒
2. 时间戳Timestamp对象的生成
pd.Timestamp(1594282374.596097,unit='s')

type(pd.Timestamp(164913461,unit='s'))
pd.Timestamp("2019-12-12")

pd.Timestamp("2019/12/12")

pd.Timestamp(2019,12,12)

pd.Timestamp(year=2019, month=12, day=12, hour=0,minute=0,second=0)

3. pd.to_datetime
处理日期序列
mydate=pd.Series(["2019/12/10","2019/12/11","2019/12/12"])
pd.to_datetime(mydate)
pd.to_datetime(mydate)[0]
pd.to_datetime("2019*12*12",format="%Y*%m*%d")
pd.date_range
s1=pd.date_range('2011-1-20', freq='H',periods=8)

s1=pd.date_range('2011-1-20', '2011-1-30',freq='D')

date=raw_data.日期.astype(str)

pd.to_datetime(date).head()

raw_data.日期.dt.strftime("%m/%d/%Y").head()#数据类型会发生了变化
raw_data.日期.dt.strftime("%Y").head()
提取时间元素
raw_data.日期.dt.year.head() #把年取出来,是一个int类型
raw_data.日期.dt.month.head() #把月调出来是一个int类型
raw_data.日期.dt.day.head() #把日调出来是一个int类型
raw_data.日期.dt.date.head() #把日期调出来,是一个object类型
raw_data.日期.dt.time.head() #把时间调出来,是一个object类型
日期索引对象DatetimeIndex
拷贝
list1=['a','b',['c','d']]
list3=list1.copy()
list3

list1=['a','b',['c','d']]
list3=list1.copy() #浅拷贝
list3[2][1]='aaaaa'
list3

import copy
list1=['a','b',['c','d']]
list3=copy.deepcopy(list1) #深拷贝
list3[2][1]='aaaaa'
list3

data.set_index(data.日期) #reindex,把原有的拉出,没有的nan
索引和切片
data["2017-09-14"]

data["2017-09-14":"2017-09-11"]

data.sort_index().head()
data.index.dayofweek+1

data.index.dayofyear

data.index.weekofyear

data.index.is_month_start

data.index.is_month_end

DateOffset对象
date = pd.Timestamp("2020-05-16")
date + pd.DateOffset(years=0,months=1, days=5) #当然可以再里面再添加years、hours等参数
移动df.shift()
index = pd.date_range("2018-6-24", periods=4, freq="W")
ts = pd.Series(['吃饭', '睡觉', '上厕所', '打豆豆'], index=index)
ts
ts.shift(2) #将数据往下移两位

ts.shift(-2) #将数据往上移两位

ts.shift(2, freq='D')

data=data.sort_index()
data.asfreq(freq='2D').head() #将频率转换为天
0.1374
3
1
关注作者
收藏
发表评论
暂无数据

