Let's say I have a time series "TS" with the range of months. How can I get a sub-time series from TS with for example the range of one day.
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'])
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.