简体   繁体   English

切片时间序列

[英]Slice a Time Series

Let's say I have a time series "TS" with the range of months. 假设我有一个月份范围的时间序列“TS”。 How can I get a sub-time series from TS with for example the range of one day. 如何从TS获取子时间序列,例如一天的范围。

I tried this code : 我试过这段代码:

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

But I get this error : 但我得到这个错误:

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

What I understood is, the method I chose matches every value from TS (472rows) an the time range I gave in the constructor (ie : ['2015-09-26','2015-09-27']) 我所理解的是,我选择的方法匹配TS(472rows)中的每个值和我在构造函数中给出的时间范围(即:['2015-09-26','2015-09-27'])

Is there a way to really slice a time series? 有没有办法真正切片时间序列? Just extract a part of it within a given time range? 只需在给定的时间范围内提取部分内容?

I think you can use selecting by [] , see also indexing : 我认为您可以使用[]选择,另请参阅索引

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

Or: 要么:

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

Sample: 样品:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM