使用 pandas 的 datetimeindex 在特定日期按分钟和小时提取时间间隔

Extracting time interval by minute and hour in a particular day using datetimeindex of pandas

假设我们有一个包含时间索引的数据帧,我们只想提取一个包含 10:23 到 14:34 的数据帧。我们该怎么做?

n =1000
i = pd.date_range('2018-04-09', periods=n, freq='1min')
ts = pd.DataFrame({'A': [i for i in range(n)]}, index=i)
print(ts)

                       A
2018-04-09 00:00:00    0
2018-04-09 00:01:00    1
2018-04-09 00:02:00    2
2018-04-09 00:03:00    3
2018-04-09 00:04:00    4
...                  ...
2018-04-09 16:35:00  995
2018-04-09 16:36:00  996
2018-04-09 16:37:00  997
2018-04-09 16:38:00  998
2018-04-09 16:39:00  999

我的尝试:

我觉得像这样的每一个问题,我们都需要把它分解成3个条件。如果我错了请纠正我。

mask1 = ( 10 == ts.index.hour & 23 <= ts.index.minute)
mask2 = ( 10 <= ts.index.hour )
mask3 = ( 14 == ts.index.hour & 34 >= ts.index.minute)

mask = mask1 | mask2 | mask3
ts_desire = ts[mask]

然后我得到TypeError: Input must be Index or array-like

更新

Why it starts from 10? It is supposed to start from 10:23 inclusive and ends at 16:34 inclusive

也许您正在寻找 between_time:

>>> ts.between_time('10:23', '16:34')

                       A
2018-04-09 10:23:00  623
2018-04-09 10:24:00  624
2018-04-09 10:25:00  625
2018-04-09 10:26:00  626
2018-04-09 10:27:00  627
...                  ...
2018-04-09 16:30:00  990
2018-04-09 16:31:00  991
2018-04-09 16:32:00  992
2018-04-09 16:33:00  993
2018-04-09 16:34:00  994

[372 rows x 1 columns]

失踪 ( )。注意运算符优先级:& 优先于 ==.

#                  HERE ----v---v
mask1 = (10 == ts.index.hour) & (23 <= ts.index.minute)
mask2 = (10 <= ts.index.hour)
mask3 = (14 == ts.index.hour) & (34 >= ts.index.minute)
#                  HERE ----^---^

mask = mask1 | mask2 | mask3
ts_desire = ts[mask]

输出:

>>> ts_desire
                       A
2018-04-09 10:00:00  600
2018-04-09 10:01:00  601
2018-04-09 10:02:00  602
2018-04-09 10:03:00  603
2018-04-09 10:04:00  604
...                  ...
2018-04-09 16:35:00  995
2018-04-09 16:36:00  996
2018-04-09 16:37:00  997
2018-04-09 16:38:00  998
2018-04-09 16:39:00  999

[400 rows x 1 columns]