Bhuvan Kumar
Bhuvan Kumar

Reputation: 564

Flag from start time till end time in pandas dataframe

I have 2 pandas DataFrame.

df1

enter image description here

DateTime
21/6/2020 0:00
21/6/2020 0:10
21/6/2020 0:20
21/6/2020 0:30
21/6/2020 0:40
21/6/2020 0:50
21/6/2020 1:00
21/6/2020 1:10
21/6/2020 1:20
21/6/2020 1:30
21/6/2020 1:40
21/6/2020 1:50
21/6/2020 2:00

df2

enter image description here

Date    Start   End
21.06.2020  0:05    0:32
21.06.2020  1:06    1:49

I should be able to flag/track 1 in df1 considering df2 start and end time. How can i do it with pandas?

Output

enter image description here

Upvotes: 0

Views: 97

Answers (1)

BENY
BENY

Reputation: 323226

Try with numpy broadcast

s=df1.DateTime.values
s1=pd.to_datetime(df2.Date+ ' ' + df2.Start).values
s2=pd.to_datetime(df2.Date+ ' ' + df2.End).values
df1['V']=np.any(((s-s1[:,None])/np.timedelta64(1, 's')>0) & ((s-s2[:,None])/np.timedelta64(1, 's')<0),0)
df1
Out[444]: 
              DateTime      V
0  2020-06-21 00:00:00  False
1  2020-06-21 00:10:00   True
2  2020-06-21 00:20:00   True
3  2020-06-21 00:30:00   True
4  2020-06-21 00:40:00  False
5  2020-06-21 00:50:00  False
6  2020-06-21 01:00:00  False
7  2020-06-21 01:10:00   True
8  2020-06-21 01:20:00   True
9  2020-06-21 01:30:00   True
10 2020-06-21 01:40:00   True
11 2020-06-21 01:50:00  False
12 2020-06-21 02:00:00  False

Upvotes: 1

Related Questions