[Generating time series time series pandas]

import pandas as pd
import numpy as np


date_range

  • You can specify the start time and period
  • H: hours
  • D: day
  • M: month
rng = pd.date_range('2016-07-01', periods = 10, freq = '3D')
rng

DatetimeIndex([‘2016-07-01’, ‘2016-07-04’, ‘2016-07-07’, ‘2016-07-10’,
‘2016-07-13’, ‘2016-07-16’, ‘2016-07-19’, ‘2016-07-22’,
‘2016-07-25’, ‘2016-07-28’],
dtype=‘datetime64[ns]’, freq=‘3D’)

time=pd.Series(np.random.randn(20),
           index=pd.date_range(dt.datetime(2016,1,1),periods=20))
print(time)

2016-01-01 -0.129379
2016-01-02 0.164480
2016-01-03 -0.639117
2016-01-04 -0.427224
2016-01-05 2.055133
2016-01-06 1.116075
2016-01-07 0.357426
2016-01-08 0.274249
2016-01-09 0.834405
2016-01-10 -0.005444
2016-01-11 -0.134409
2016-01-12 0.249318
2016-01-13 -0.297842
2016-01-14 -0.128514
2016-01-15 0.063690
2016-01-16 -2.246031
2016-01-17 0.359552
2016-01-18 0.383030
2016-01-19 0.402717
2016-01-20 -0.694068
Freq: D, dtype: float64

truncate filter

time.truncate(before='2016-1-10')

2016-01-10 -0.005444
2016-01-11 -0.134409
2016-01-12 0.249318
2016-01-13 -0.297842
2016-01-14 -0.128514
2016-01-15 0.063690
2016-01-16 -2.246031
2016-01-17 0.359552
2016-01-18 0.383030
2016-01-19 0.402717
2016-01-20 -0.694068
Freq: D, dtype: float64

time.truncate(after='2016-1-10')

2016-01-01 -0.129379
2016-01-02 0.164480
2016-01-03 -0.639117
2016-01-04 -0.427224
2016-01-05 2.055133
2016-01-06 1.116075
2016-01-07 0.357426
2016-01-08 0.274249
2016-01-09 0.834405
2016-01-10 -0.005444
Freq: D, dtype: float64
Here Insert Picture Description

#时间戳
pd.Timestamp('2016-07-10')
# TIME OFFSETS
pd.Timedelta('1 day')
pd.Period('2016-01-01 10:10') + pd.Timedelta('1 day')

Period(‘2016-01-02 10:10’, ‘T’)

Published 116 original articles · won praise 10 · views 1350

Guess you like

Origin blog.csdn.net/weixin_44727383/article/details/105007154