Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
870 views
in Technique[技术] by (71.8m points)

python - Resample hourly TimeSeries with certain starting hour

I want to resample a TimeSeries in daily (exactly 24 hours) frequence starting at a certain hour.

Like:

index = date_range(datetime(2012,1,1,17), freq='H', periods=60)

ts = Series(data=[1]*60, index=index)

ts.resample(rule='D', how='sum', closed='left', label='left')

Result i get:

2012-01-01  7
2012-01-02 24
2012-01-03 24
2012-01-04  5
Freq: D

Result i wish:

2012-01-01 17:00:00 24
2012-01-02 17:00:00 24
2012-01-03 17:00:00 12
Freq: D

Some weeks ago you could pass '24H' to the freq argument and it worked totally fine. But now it combines '24H' to '1D'.

Was I using a bug with '24H' which is fixed now? And how can i get the wished result in a efficient and pythonic (or pandas) way back?

versions:

  • python 2.7.3
  • pandas 0.9.0rc1 (but doesn't work in 0.8.1, too)
  • numpy 1.6.1
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Resample has an base argument which covers this case:

ts.resample(rule='24H', closed='left', label='left', base=17).sum()

Output:

2012-01-01 17:00:00    24
2012-01-02 17:00:00    24
2012-01-03 17:00:00    12
Freq: 24H

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...