时间操作
import datetime
import pandas as pd
dt = datetime.datetime(year=2020,month=10,day=11,hour=13,minute=10)
dt
datetime.datetime(2020, 10, 11, 13, 10)
print (dt)
2020-10-11 13:10:00
ts = pd.Timestamp('2020-10-11')
ts
Timestamp('2020-10-11 00:00:00')
ts.month
10
ts.day
11
ts + pd.Timedelta('5 days')
Timestamp('2020-10-16 00:00:00')
pd.to_datetime('2020-10-11')
pd.to_datetime('11/10/2020')
Timestamp('2020-11-10 00:00:00')
s = pd.Series(['2020-10-11 00:00:00','2020-10-12 00:00:00','2020-10-13 00:00:00'])
s
0 2020-10-11 00:00:00
1 2020-10-12 00:00:00
2 2020-10-13 00:00:00
dtype: object
ts = pd.to_datetime(s)
ts
0 2020-10-11
1 2020-10-12
2 2020-10-13
dtype: datetime64[ns]
ts.dt.weekday
0 6
1 0
2 1
dtype: int64
构建时间序列
pd.Series(pd.date_range(start='2020-10-11',periods = 10,freq = '6H'))
0 2020-10-11 00:00:00
1 2020-10-11 06:00:00
2 2020-10-11 12:00:00
3 2020-10-11 18:00:00
4 2020-10-12 00:00:00
5 2020-10-12 06:00:00
6 2020-10-12 12:00:00
7 2020-10-12 18:00:00
8 2020-10-13 00:00:00
9 2020-10-13 06:00:00
dtype: datetime64[ns]
data = pd.read_csv('./data/flowdata.csv')
data['Time'] = pd.to_datatime(date['Time'])
data = data.set_index('Time')
data.head(10)
data = pd.read_csv('./data/flowdata.csv',index_col = 0,parse_dates = True)
data.head(10)
data[pd.Timestamp('2012-01-01 09:00'):pd.Timestamp('2012-01-01 19:00')]
data[('2012-01-01 09:00'):('2012-01-01 19:00')]
data['2013']
data['2012-01':'2012-03']
data[data.index.month == 1]
data[(data.index.hour > 8) & (data.index.hour <12)]
data.between_time('08:00','12:00')
data.resample('D').mean().head()
data.resample('3D').mean().head()
data.resample('M').mean().head()
data.resample('Y').mean().head()
data.resample('M').mean().plot()