2017-06-15 77 views
1

比方說,我有一個時間系列「TS」的月份範圍。 如何從TS中獲得一個子時間序列,例如一天的範圍。切片時間序列

我試過這段代碼:

subts = pd.Series(TS,index=pd.to_datetime(['2015-09-26','2015-09-27'])) 

但我得到這個錯誤:

ValueError: Wrong number of items passed 472, placement implies 2 

我的理解是,該方法我選擇比賽從TS(472rows)每個值的時間範圍我給了構造函數(即:['2015-09-26','2015-09-27'])

有沒有一種方法來真正切片的時間序列?只需在給定的時間範圍內提取其中的一部分?

回答

0

我想你可以通過[]使用選擇,也見indexing

subts = TS['2015-09-26':'2015-09-27'] 

或者:

subts = TS.loc['2015-09-26':'2015-09-27'] 

樣品:

np.random.seed(123) 
TS = pd.Series(np.random.randint(10, size=10), pd.date_range('2015-09-24', periods=10)) 
print (TS) 
2015-09-24 2 
2015-09-25 2 
2015-09-26 6 
2015-09-27 1 
2015-09-28 3 
2015-09-29 9 
2015-09-30 6 
2015-10-01 1 
2015-10-02 0 
2015-10-03 1 
Freq: D, dtype: int32 

subts = TS['2015-09-26':'2015-09-27'] 
print (subts) 
2015-09-26 6 
2015-09-27 1 
Freq: D, dtype: int32