Suppose we have a dataframe including time indices and we want to extract only a dataframe including 10:23 to 14:34. How can we do this?
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
My try:
I think for every problem like this, we need to break it into 3 conditions. Correct me if I am wrong.
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]
Then I get TypeError: Input must be Index or array-like
.