- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有两个数据帧,它们都包含一个不规则间隔的毫秒分辨率时间戳列。我的目标是匹配行,以便对于每个匹配的行,1)第一个时间戳总是小于或等于第二个时间戳,以及 2)匹配的时间戳对于满足 1)的所有时间戳对是最接近的。
有没有办法用 pandas.merge 做到这一点?
最佳答案
merge()
不能做这种join,但是可以使用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()
调用 searchsorted()
为 t2
中的每个值在 t1
中查找索引:
idx = np.searchsorted(t1, t2) - 1
mask = idx >= 0
df = pd.DataFrame({"t1":t1[idx][mask], "t2":t2[mask]})
这是输出:
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
通过图表查看此结果:
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)
输出:
绿线是t1
,蓝线是t2
,红线是从t1
中选出每个t2
.
关于python - pandas.merge : match the nearest time stamp >= the series of timestamps,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21201618/
我们在一些 .NET 项目中使用 Stamp.Fody 包,它们有时会间歇性地失败构建并出现以下错误:Fody:无法在 1 秒内将产品版本应用于 Win32 资源。 显然 1第二个是默认超时,如果 v
Masonry 3 添加了一种方法“stamp”,它非常适合将某个元素固定到位。但是,它似乎无法正常工作/正如我所期望的那样。 以 David DeSandro 本人为例: http://codepe
所以,我一直在寻找解决以下问题的算法: 给你一个想要的字符串s,和一个标记t。 t 也是一个字符串。让起始字符串为 len(s)*"?"。 是否可以使用戳记将开始的字符串转换为使用戳记的字符串s?整个
在 Bungie 的演示文稿(Bungie 农场的生活)中,提到了 Visual Studio 中名为“Source Stamping”的功能。我想阅读更多有关该主题的信息,但很难在 MSDN 上找到
我在数据库中有 start_time 和 end_time 列,其中包含毫秒值。 start_time > 2018 年 2 月 14 日星期三 11:00:00(真实日期)= 15185862000
最近遇到过一个这样的问题,我在我的Mac机器将一个刚刚创建的文件进行tar打包生成文件scripts.tar.bz2,然后在一台Centos得到这个文件,进行解压,然后出现了如下问题 time st
我的数据框如下所示:df= POA ... Inverter efficiency 2019-01-
我在 codeigniter 工作。我需要获取两个时间戳之间的差异,并将其用于忘记密码服务中的链接到期。 到目前为止我已经尝试过。 $date= date('Y-m-d H:i'); 这将产生类似 2
我正在用 Java 编写一个桌面应用程序,我希望本地网络数据库上的许多用户可以使用它。我有一个登录框架。 问题是,每次我想编写查询时,我都必须获得一个新连接。 第一个问题是:我如何才能让我的连接方法记
这是问题的链接:http://www.spoj.com/problems/STAMPS/ ;这是当前代码的 ideone 链接:http://ideone.com/AcHfc6 ;这是代码: #inc
我发现很多关于旋转的问题都涉及页面旋转 - 充其量是图像旋转。矩形和图章注释等对象究竟是如何通过 iText 旋转的? 我最初预计会有一个高级方法,例如 Rectangle.SetRotation()
我是 opencv 的新手。我有多个图像。示例图像之一如下图左上角所示。基本上我想分开背景和前景,这样边缘就清晰了,我就可以正确地检测到轮廓。 我尝试了很多过滤器,当然还有使用各种参数的阈值。 最后,
我正在为我的电子商务商店从 Endicia 转换到 Stamps.com。对于一流的国际标签,Stamps.com 目前强制标签采用标准 8x11 尺寸文档中的 PDF 格式。这可以防止标签直接打印到
我在迈拓硬盘中有多伦多证券交易所的股票数据。数据是一个包含 .dat 和 .pos 组件的 TBF 文件。 .dat文件包含所有二进制格式的Stamp格式传输信息。 我可以使用 R 读取 .pos 文
在 CSS 中是否有任何有效的方法来呈现具有使其看起来像橡皮图章效果的字体?就像橡皮图章上的墨水覆盖在打印 Material 上一样? 更好的方法是将相同的效果应用于边框,就好像边框是橡皮图章的一部分
引用: 我想弄清楚“Handy Time Stamp”在时间包中的用途。 我可以使用其他常量(例如 RFC)很好地解析日期 t, _ := time.Parse(time.RFC822, "02 Ja
我正在尝试更新此脚本以在 stamps.com 服务器上使用。 ssx_customers.name 值来自另一个表。 SELECT contacts_cstm.num9c a
我正在阅读目录以查找特定文件名。我可以从每个文件名中删除文档标记 '.xml' 以进行比较。问题是其中大约 10% 的标题末尾有一个六位数的时间戳。 file_list = os.listdir(di
我正在尝试将 QDateTime 转换为 Unix 时间戳: QDateTime PubDate = audioFile->GetPublishingdate(); uint UTC_Time = P
我最近总体上接触了 iText -- 看过第 5 版和第 7 版后,我仍然对图章的实现方式感到困惑。我按照使用 iTextSharp 7 的示例代码之一来添加注释: PdfReader reader
我是一名优秀的程序员,十分优秀!