Márius Rak
Márius Rak

Reputation: 1472

Comparing many timestamps with pandas

I have two dataframes with different sizes containing timestamps. I need to find nearest timestamps. In df A I need to find all first timestamps after any of timestamps of df B. The dataframes have each around 100,000 rows so iteration is not a way and even df.apply() took around 6 mins.

e.g.:

A:
11
12
15
16
18
20
25
30
50

B:
14
19
22
27

result:
15
20
25
30

Upvotes: 0

Views: 42

Answers (1)

jezrael
jezrael

Reputation: 862641

Use Series.searchsorted:

out = a.loc[a['A'].searchsorted(b['B']), 'A']
print (out)
2    15
5    20
6    25
7    30
Name: A, dtype: int64

Upvotes: 2

Related Questions