4

I have been using the between_time method of TimeSeries in pandas, which returns all values between the specified times, regardless of their date.

But I need to select both date and time, because my timeseries structure contains multiple dates.

One way of solving this, though quite inflexible, is just iterate over the values and remove those which are not relevant.

Is there a more elegant way of doing this ?

4

1 回答 1

6

您可以先选择感兴趣的日期,然后使用between_time. 例如,假设您有一个 72 小时的时间序列:

import pandas as pd
from numpy.random import randn

rng = pd.date_range('1/1/2013', periods=72, freq='H')
ts = pd.Series(randn(len(rng)), index=rng)

要选择 1 月 2 日和 3 日 20:00 和 22:00 之间的时间,您只需执行以下操作:

ts['2013-01-02':'2013-01-03'].between_time('20:00', '22:00')

给你这样的东西:

2013-01-02 20:00:00    0.144399
2013-01-02 21:00:00    0.886806
2013-01-02 22:00:00    0.126844
2013-01-03 20:00:00   -0.464741
2013-01-03 21:00:00    1.856746
2013-01-03 22:00:00   -0.286726
于 2013-06-09T17:24:16.670 回答