1.time模块
1.时间戳 1970年1月1日0时0分0秒到现在时间偏移量
time.time()
>>1661825563.8726559
time.ctime(time.time())
>>'Tue Aug 30 10:39:32 2022'
2. 结构化时间当地时间
t = time.localtime(time.time())
t
>>'2022-08-30 10:17:07'
t.tm_year
time.gmtime(time.time())
>>time.struct_time(tm_year=2022, tm_mon=8, tm_mday=30, tm_hour=2, tm_min=37, tm_sec=24, tm_wday=1, tm_yday=242, tm_isdst=0)
time.strftime("%Y-%m-%d %H:%M:%S",t)
>>'2022-08-30 10:17:07'
time.strptime('2021-12-10 21:24:22',"%Y-%m-%d %H:%M:%S")
>>time.struct_time(tm_year=2021, tm_mon=12, tm_mday=10, tm_hour=21, tm_min=24, tm_sec=22, tm_wday=4, tm_yday=344, tm_isdst=-1)
time.truncate(before='2014-3-13')
time.truncate(after='2014-3-13')
3. 求时间差
start_time = '2021-8-27 0:00:00'
end_time = '2022-8-30 0:00:00'
start_time1 = time.strptime(start_time,"%Y-%m-%d %H:%M:%S")
end_time1 = time.strptime(end_time,"%Y-%m-%d %H:%M:%S")
t = time.mktime(end_time1) - time.mktime(start_time1)
t_data = time.gmtime(t)
t_data
"时间差是%s年%s月%s日,%s时%s分%s秒"%(t_data.tm_year-1970,t_data.tm_mon-1,t_data.tm_mday-1,t_data.tm_hour,t_data.tm_min,t_data.tm_sec)
2.datetime模块
- from datetime import datetime
1.当地时间
now = datetime.now()
now
>>datetime.datetime(2022, 8, 30, 11, 20, 36, 116742)
now.year,now.month,now.day
>>(2021, 12, 13)
2. 求时间差
d = datetime(2021,12,10,20,0,0) - datetime(2021,12,9,1,0,0)
d
>>datetime.timedelta(1, 68400)
d.days,d.seconds
>>(1, 68400)
- from datetime import timedelta
- 现在时间加一天
start = datetime(2021,8,12)
start + timedelta(1,3600)
>>datetime.datetime(2021, 8, 13, 1, 0)
a = datetime.today()+timedelta(1)
c = a.strftime('%Y-%m-%d')
c
>> datetime.datetime(2022, 9, 30, 16, 0, 39, 725040)
>> '2022-10-01'
3. 将表格某一时间列转化为datetime对象
df['data_date'] = pd.to_datetime(df['data_date'])
df['月'] = df['data_date'].dt.month
<< 生成月份的列
df['日'] = df['data_date'].dt.year
<< 生成日的列
3.datetime 和字符串相互的转换
stamp = datetime(2021,8,12)
stamp
>>datetime.datetime(2021, 8, 12, 0, 0)
str(stamp)
stamp.strftime("%Y-%m-%d %H:%M:%S")
>>'2021-08-12 00:00:00'
d = ['12/12/2021','1/1/2021']
d
>>['12/12/2021', '1/1/2021']
[datetime.strptime(i,"%m/%d/%Y") for i in d ]
>>[datetime.datetime(2021, 12, 12, 0, 0), datetime.datetime(2021, 1, 1, 0, 0)]
- 字符串转换成datetime
- from dateutil.parser import parse
parse('12/12/2021')
parse('2021-12-12')
parse('2021.12.12')
parse('2021 12 12')
>>datetime.datetime(2021, 12, 12, 0, 0)
4.时间序列
- 1.生成一个时间序列的Series对象以及DataFrame对象
import pandas as pd
import numpy as np
from datetime import datetime
dates = [datetime(2021,12,12),datetime(2021,12,13),datetime(2021,12,14),datetime(2021,12,15),datetime(2021,12,16)]
dates
>>[datetime.datetime(2021, 12, 12, 0, 0),
datetime.datetime(2021, 12, 13, 0, 0),
datetime.datetime(2021, 12, 14, 0, 0),
datetime.datetime(2021, 12, 15, 0, 0),
datetime.datetime(2021, 12, 16, 0, 0)]
ts = pd.Series(np.random.randn(5),index=dates)
ts
>>2021-12-12 -0.564338
2021-12-13 1.199842
2021-12-14 2.512560
2021-12-15 0.739945
2021-12-16 -0.483202
dtype: float64
a = pd.DataFrame({"zhi":ts})
a
>> zhi
2021-12-12 -0.564338
2021-12-13 1.199842
2021-12-14 2.512560
2021-12-15 0.739945
2021-12-16 -0.483202
a.index
ts.index
>>DatetimeIndex(['2021-12-12', '2021-12-13', '2021-12-14', '2021-12-15',
'2021-12-16'],
dtype='datetime64[ns]', freq=None)
ts['2021-12-12']
ts['2021 12 12']
ts['2021.12.12']
ts['2021/12/12']
>>-0.5168556782608846
pd.date_range('2021/1/1',periods=1000)
>>DatetimeIndex(['2021-01-01', '2021-01-02', '2021-01-03', '2021-01-04',
'2021-01-05', '2021-01-06', '2021-01-07', '2021-01-08',
'2021-01-09', '2021-01-10',
...
'2023-09-18', '2023-09-19', '2023-09-20', '2023-09-21',
'2023-09-22', '2023-09-23', '2023-09-24', '2023-09-25',
'2023-09-26', '2023-09-27'],
dtype='datetime64[ns]', length=1000, freq='D')
ts1 = pd.Series(np.random.randn(1000),index=pd.date_range('2021/1/1',periods=1000))
ts1
ts1['2021']
ts['2021-01']
ts1['2021-01'].sum()
- 查找2021/8/12到2021/8/19的所有数据
ts1['8/12/2021':'8/19/2021']
>>2021-08-12 -1.154767
2021-08-13 -1.082007
2021-08-14 1.656297
2021-08-15 -0.363210
2021-08-16 0.085454
2021-08-17 -1.214048
2021-08-18 -0.674199
2021-08-19 -0.002772
Freq: D, dtype: float64
ts1.truncate(after='1/6/2021')
>>2021-01-01 -1.384203
2021-01-02 -1.717038
2021-01-03 -1.929252
2021-01-04 0.703907
2021-01-05 -0.150625
2021-01-06 -0.773766
Freq: D, dtype: float64
ts1.truncate(before='2023/7/8')
5.日期范围
index = pd.date_range('2021-01-01','2021-03-01')
index
pd.date_range('2021-01-01',periods=100)
pd.date_range(end='2021-01-11',periods=100)
pd.date_range('2021-08-01','2021-08-12',freq='3s')
ts = pd.Series(np.random.randn(4),
index=pd.date_range('1/1/2000', periods=4, freq='M'))
ts
6.重采样
t = pd.DataFrame(np.random.uniform(10,50,(100,1)),index=pd.date_range('20170101',periods=100))
t
t.resample('M').sum()
>>
0
2017-01-31 957.657756
2017-02-28 837.778145
2017-03-31 851.612023
2017-04-30 308.126704
t.resample('10D').sum()
frame = pd.DataFrame(np.random.randn(2, 4),
index=pd.date_range('1/1/2000', periods=2,freq='W-WED'),
columns=['上海', '北京', '深圳', '广州'])
frame
frame.resample('D').asfreq()
frame.resample('D').ffill()
frame.resample('D').bfill()