文章目录
1. date_range
- 时间戳 ( timestamp )
- 固定周期 ( period )
- 时间间隔 ( interval )
可以指定开始时间与周期
- H:小时
- D:天
- M:月
import numpy as np
import pandas as pd
rng = pd.date_range('2018-08-08', periods=10, freq='3D')
rng
import datetime
time = pd.Series(np.random.randn(20), index=pd.date_range(datetime.datetime(2018,8,8),periods=20))
time
2018-08-08 -0.116898
2018-08-09 0.236001
2018-08-10 0.465807
…
2018-08-26 1.008301
2018-08-27 0.225361
Freq: D, dtype: float64
2. truncate 过滤
time.truncate(before = '2018-8-15')
time.truncate(after = '2018-8-15')
2018-08-15 -1.244359
2018-08-16 1.043819
2018-08-17 1.870143
…
2018-08-26 1.008301
2018-08-27 0.225361
Freq: D, dtype: float64
time['2018-8-10' : '2018-8-14']
2018-08-10 0.465807
2018-08-11 1.365110
2018-08-12 -2.545710
2018-08-13 1.568111
2018-08-15 -1.244359
Freq: D, dtype: float64
data = pd.date_range('2018-1-1', '2019-1-1', freq='M')
data
3. Timestamp, Period, Timedelta
3.1 Timestamp 时间戳
print(pd.Timestamp('2019-9-9 10'))
pd.Timestamp('2019-9-9 10:15')
2019-09-09 10:00:00
Times