为 pandas 创建自定义插值函数

2024-04-24

我目前正在尝试使用 pandas 清理和填充一些缺失的时间序列数据。插值函数工作得很好,但是它没有我的数据集所需的一些(不太广泛使用的)插值函数。几个例子是一个简单的“最后”有效数据点,它会创建类似于阶跃函数的东西,或者类似对数或几何插值的东西。

浏览文档,似乎没有办法传递自定义插值函数。这样的功能直接存在于 pandas 中吗?如果没有,是否有人做过 pandas-fu 来通过其他方式有效地应用自定义插值?


Pandas 提供的插值方法是scipy.interpolate.interp1d https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp1d.html- 不幸的是,这似乎无法以任何方式扩展。我必须做类似的事情来应用 SLERP 四元数插值(使用numpy 四元数 https://github.com/moble/quaternion),而且我成功地做到了这一点。我将在此处复制代码,希望您可以根据您的目的进行调整:

def interpolate_slerp(data):
    if data.shape[1] != 4:
        raise ValueError('Need exactly 4 values for SLERP')
    vals = data.values.copy()
    # quaternions has size Nx1 (each quaternion is a scalar value)
    quaternions = quaternion.as_quat_array(vals)
    # This is a mask of the elements that are NaN
    empty = np.any(np.isnan(vals), axis=1)
    # These are the positions of the valid values
    valid_loc = np.argwhere(~empty).squeeze(axis=-1)
    # These are the indices (e.g. time) of the valid values
    valid_index = data.index[valid_loc].values
    # These are the valid values
    valid_quaternions = quaternions[valid_loc]
    # Positions of the missing values
    empty_loc = np.argwhere(empty).squeeze(axis=-1)
    # Missing values before first or after last valid are discarded
    empty_loc = empty_loc[(empty_loc > valid_loc.min()) & (empty_loc < valid_loc.max())]
    # Index value for missing values
    empty_index = data.index[empty_loc].values
    # Important bit! This tells you the which valid values must be used as interpolation ends for each missing value
    interp_loc_end = np.searchsorted(valid_loc, empty_loc)
    interp_loc_start = interp_loc_end - 1
    # These are the actual values of the interpolation ends
    interp_q_start = valid_quaternions[interp_loc_start]
    interp_q_end = valid_quaternions[interp_loc_end]
    # And these are the indices (e.g. time) of the interpolation ends
    interp_t_start = valid_index[interp_loc_start]
    interp_t_end = valid_index[interp_loc_end]
    # This performs the actual interpolation
    # For each missing value, you have:
    #   * Initial interpolation value
    #   * Final interpolation value
    #   * Initial interpolation index
    #   * Final interpolation index
    #   * Missing value index
    interpolated = quaternion.slerp(interp_q_start, interp_q_end, interp_t_start, interp_t_end, empty_index)
    # This puts the interpolated values into place
    data = data.copy()
    data.iloc[empty_loc] = quaternion.as_float_array(interpolated)
    return data

诀窍在于np.searchsorted,它很快就能找到每个值的正确插值终点。该方法的局限性在于:

  • 您的插值函数必须有效somewhat like quaternion.slerp(这不应该奇怪,因为它有常规的 ufunc 广播行为)。
  • 它仅适用于每一端仅需要一个值的插值方法,因此如果您想要例如像三次插值这样的东西(你不需要,因为已经提供了),这是行不通的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为 pandas 创建自定义插值函数 的相关文章

  • Django:如何从管理界面调用管理自定义命令执行?

    参考 从代码执行管理命令 https stackoverflow com questions 907506 how can i call a custom django manage py command directly from a t
  • Python - 重写 print()

    我正在使用 mod wsgi 想知道是否可以覆盖 print 命令 因为它没用 这样做是行不通的 print myPrintFunction 因为这是一个语法错误 Print 不是 Python 2 x 中的函数 因此这不能直接实现 但是
  • 使用数据库数据模型生成 SQLAlchemy 模型、架构和 JSON 响应

    将 Flask 和 SQLAlchemy 用于 Python Web 应用程序 我的目标是创建一个系统 在其中我可以 从现有 PostgreSQL 数据库导入数据模型 并将它们映射到相应 SQLAlchemy 模型中的字段 使用这些 SQL
  • 抓取多个帐户,即多次登录

    我可以成功抓取单个帐户的数据 我想在一个网站上抓取多个帐户 这意味着多次登录 如何管理登录 注销 您可以在每个帐户会话中使用多个 cookiejar 并行抓取多个帐户 请参阅 cookiejar 请求元密钥http doc scrapy o
  • Python 有哪些 SOAP 客户端库,它们的文档在哪里? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 向 list.extend() 传递不可迭代对象

    我正在创建一个公共方法来允许调用者将值写入设备 例如将其称为 write vals 由于这些值将实时输入 因此我希望通过允许用户输入列表或单个值来简化用户的生活 具体取决于他们需要写入的值的数量 例如 write to device 1 2
  • 无法写入文本文件

    我正在运行一些测试并需要写入文件 当我运行测试时open file r 不写入文件 测试脚本如下 class GetDetailsIP TestGet def runTest self self category PTZ try This
  • Python 2to3 Windows CMD

    我已经安装了 python 32 包到 C python32 我还设置了路径 Python 路径 C Python32 Lib C Python32 DLLs C Python32 Lib lib tk 路径 C Python32 我想使用
  • 如何在 Django 中创建多选框?

    我正在尝试创建多选框字段来自姜戈选择 2 https github com applegrew django select2库如下图所示 我使用了下一个代码 但它返回简单的选择多个小部件 我想我忘了补充一些东西 我的错误在哪里 有人可以告诉
  • PySide2/QML 填充 Gridview 模型/委托并为其设置动画

    我是 QML 的新手 正在寻求以下几点帮助 如何基于 TextField 输入 如 Regex 通过 PySide2 过滤 Gridview 模型中的 QAbstractListModel 数据 标题 如何在鼠标悬停时为 Gridview
  • 将 csv 写入谷歌云存储

    我试图了解如何将多行 csv 文件写入谷歌云存储 我只是没有遵循文档 https googlecloudplatform github io google cloud python stable storage blobs html hig
  • 使用 Python 3.7+ 中的 wfastcgi 以及 Numpy、Pandas 等在 IIS 上部署 Python Flask 应用程序

    使用 wfastcgi 在 IIS 上部署 python 3 7 Flask 或 Dash 应用程序时 有许多很棒的教程可以让 hello work 程序正常运行 例如 https medium com bilalbayasut deplo
  • 为什么 Python 的 argparse 对 SystemExit 使用错误代码 2?

    当我给 Python 的 argparse 输入它不喜欢的输入时 它会引发一个代码为 2 的 SystemExit 其中似乎意味着 没有这样的文件或目录 https docs python org 2 library errno html
  • PyCharm 可以列出项目中的所有 Python 错误吗?

    我在虚拟环境中使用 python 2 7 和 PyCharm 2 7 2013 年 2 月 7 日的新版本 每当我打开其中有明确错误的Python文件 相当于其他语言中的编译错误 例如使用未声明的变量 调用不存在的函数 时 它会在文件的装订
  • 在 matplotlib 中添加新的导航模式

    我正在编写一个 wx matplotlib 应用程序 并且在向 matplotlib 导航工具栏添加新工具时遇到相当大的困难 基本上我想添加选择工具 选取框 套索等 以切换受控子图的鼠标模式 到目前为止 我还没有找到任何功能可以让我轻松地做
  • 如何从已安装的云端硬盘文件夹中永久删除?

    我编写了一个脚本 在每次迭代后将我的模型和训练示例上传到 Google Drive 以防发生崩溃或任何阻止笔记本运行的情况 如下所示 drive path drive My Drive Colab Notebooks models if p
  • mpld3图,注释问题

    我正在使用 mpld3 在 Intranet 网站上显示图形 我正在使用将图形保存到字典并使用 mpld3 js 在客户端渲染它的选项 除非我想使用注释 否则该图呈现良好 这些显然是抵消的 我不明白为什么 因为即使我将偏移量设置为 0 0
  • 设置restrict_xpaths设置后出现UnicodeEncodeError

    我是 python 和 scrapy 的新手 将restrict xpaths 设置设置为 table class lista 后 我收到了以下回溯 奇怪的是 通过使用其他 xpath 规则 爬虫可以正常工作 Traceback most
  • Python二进制数据读取

    urllib2 请求接收二进制响应 如下所示 00 00 00 01 00 04 41 4D 54 44 00 00 00 00 02 41 97 33 33 41 99 5C 29 41 90 3D 71 41 91 D7 0A 47 0
  • 在 python 中使用 org.mpris.mediaplayer2.player PlaybackStatus 属性

    The 规格页 http specifications freedesktop org mpris spec latest Player Interface html summary对于这个特定的接口说 PlaybackStatus s P

随机推荐