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
427 views
in Technique[技术] by (71.8m points)

python - Create hourly/minutely time range using pandas

Is there a way to generate time range in pandas similar to date_range? something like:

pandas.time_range("11:00", "21:30", freq="30min")
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

A time range doesn't exist as a standalone index type. Generate using a single date

In [1]: pandas.date_range("11:00", "21:30", freq="30min")
Out[1]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[2013-07-14 11:00:00, ..., 2013-07-14 21:30:00]
Length: 22, Freq: 30T, Timezone: None

The time objects

In [2]: pandas.date_range("11:00", "21:30", freq="30min").time
Out[2]: 
array([datetime.time(11, 0), datetime.time(11, 30), datetime.time(12, 0),
       datetime.time(12, 30), datetime.time(13, 0), datetime.time(13, 30),
       datetime.time(14, 0), datetime.time(14, 30), datetime.time(15, 0),
       datetime.time(15, 30), datetime.time(16, 0), datetime.time(16, 30),
       datetime.time(17, 0), datetime.time(17, 30), datetime.time(18, 0),
       datetime.time(18, 30), datetime.time(19, 0), datetime.time(19, 30),
       datetime.time(20, 0), datetime.time(20, 30), datetime.time(21, 0),
       datetime.time(21, 30)], dtype=object)

You can also resample if you are spanning multiple dates.

What are you trying to do?


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

...