我有两个数据帧,两个数据帧都包含一个不规则间隔的毫秒分辨率时间戳列.我的目标是匹配行,以便对于每个匹配的行,1)第一个时间戳总是小于或等于第二个时间戳,2)匹配的时间戳对于满足1)的所有时间戳对最接近.
有没有办法用pandas.merge做到这一点?
HYR*_*YRY 29
merge()不能做这种加入,但你可以使用searchsorted():
创建一些随机时间戳:t1,t2,有按升序排列:
import pandas as pd
import numpy as np
np.random.seed(0)
base = np.array(["2013-01-01 00:00:00"], "datetime64[ns]")
a = (np.random.rand(30)*1000000*1000).astype(np.int64)*1000000
t1 = base + a
t1.sort()
b = (np.random.rand(10)*1000000*1000).astype(np.int64)*1000000
t2 = base + b
t2.sort()
Run Code Online (Sandbox Code Playgroud)
调用searchsorted()以查找t1每个值的索引t2:
idx = np.searchsorted(t1, t2) - 1
mask = idx >= 0
df = pd.DataFrame({"t1":t1[idx][mask], "t2":t2[mask]})
Run Code Online (Sandbox Code Playgroud)
这是输出:
t1 t2
0 2013-01-02 06:49:13.287000 2013-01-03 16:29:15.612000
1 2013-01-05 16:33:07.211000 2013-01-05 21:42:30.332000
2 2013-01-07 04:47:24.561000 2013-01-07 04:53:53.948000
3 2013-01-07 14:26:03.376000 2013-01-07 17:01:35.722000
4 2013-01-07 14:26:03.376000 2013-01-07 18:22:13.996000
5 2013-01-07 14:26:03.376000 2013-01-07 18:33:55.497000
6 2013-01-08 02:24:54.113000 2013-01-08 12:23:40.299000
7 2013-01-08 21:39:49.366000 2013-01-09 14:03:53.689000
8 2013-01-11 08:06:36.638000 2013-01-11 13:09:08.078000
Run Code Online (Sandbox Code Playgroud)
要按图查看此结果:
import pylab as pl
pl.figure(figsize=(18, 4))
pl.vlines(pd.Series(t1), 0, 1, colors="g", lw=1)
pl.vlines(df.t1, 0.3, 0.7, colors="r", lw=2)
pl.vlines(df.t2, 0.3, 0.7, colors="b", lw=2)
pl.margins(0.02)
Run Code Online (Sandbox Code Playgroud)
输出:

绿线是t1,蓝线是t2,t1每行都选择红线t2.
| 归档时间: |
|
| 查看次数: |
11720 次 |
| 最近记录: |