cuDF 字符串与定点数互转指南:pylibcudf `strings.convert.convert_fixed_point` 模块详解 数据分析数据工程机器学习【免费下载链接】cudfcuDF - GPU DataFrame Library项目地址https://gitcode.com/gh_mirrors/cu/cudf点击查看免费下载本指南以 pylibcudf 文档 convert_fixed_point.rst 为骨架系统讲解pylibcudf.strings.convert.convert_fixed_point模块的三大核心 APIto_fixed_point字符串→定点数、from_fixed_point定点数→字符串与is_fixed_point合法性/溢出预检。读完本文你将掌握在 GPU DataFrame 中完成十进制字符串与 DECIMAL 定点列双向转换的完整调用方式、格式约定、scale 语义与底层实现原理。模块定位与文档背景convert_fixed_point是 pylibcudf 字符串转换子包strings.convert的九个成员之一其余包括convert_booleans、convert_datetime、convert_durations、convert_floats、convert_integers、convert_ipv4、convert_lists、convert_urls参见 convert/index.rst 与 convert/init.py。该模块专门处理字符串列与定点fixed-point十进制列之间的相互转换是解析日志、账单、科学计数数据中十进制文本的典型入口。模块的 Python 层声明位于 convert_fixed_point.pyx它通过 Cython 封装 convert_fixed_point.pxd 中对 libcudf C 头文件 convert_fixed_point.hpp 的导入最终调用 CUDA 内核在 GPU 上完成批量解析。模块导出的公开符号为__all__ [from_fixed_point, is_fixed_point, to_fixed_point]。核心 API 一览函数方向输入输出说明to_fixed_point(input, output_type, streamNone, mrNone)字符串 → 定点列stringsColumn 定点DataType含 scale指定类型的定点Column按格式解析十进制文本溢出不检查from_fixed_point(input, streamNone, mrNone)定点列 → 字符串定点ColumnstringsColumn依据列的 scale 放置小数点负数带-前缀is_fixed_point(input, decimal_typeNone, streamNone, mrNone)预检stringsColumn 可选定点DataType布尔Column逐字符串判定可转换性、格式与溢出默认Decimal64三个函数的底层实现均遵循统一模式通过_get_stream(stream)获取 CUDA 流、_get_memory_resource(mr)获取设备内存资源在with nogil块内调用对应 C 接口最后由Column.from_libcudf(...)将结果封装回 pylibcudfColumn见 convert_fixed_point.pyx。to_fixed_point将字符串解析为定点列函数签名与参数to_fixed_point(input: Column, output_type: DataType, stream: CudaStreamLike | None None, mr: DeviceMemoryResource | None None) - Columninput待解析的 stringsColumn。output_type目标定点列类型必须包含 scale 值例如Decimal32/Decimal64/Decimal128及对应的 scale。stream/mr可选 CUDA 流与 RMM 设备内存资源用于控制执行流与结果列的内存分配。解析格式约定C 层文档明确给出了输入格式[sign][integer][.][fraction]其中符号位可选允许-或小数点[.]可有可无integer与fraction由 0 个或多个[0-9]数字组成输入列的 null 条目在输出列中对应为 null无效格式对应的输出行为是未定义undefined behavior因此生产代码应先用is_fixed_point过滤溢出不检查超出目标定点类型表示范围的值不会被拦截。官方伪代码示例C 头文件中的示例直观展示了 scale 的作用convert_fixed_point.hpps [123, -876, 543.2, -0.12] datatype {DECIMAL32, scale-2} fp to_fixed_point(s, datatype) fp is [123400, -87600, 54320, -12]scale 为 -2 意味着小数点向左移动 2 位即保留两位小数123→123.00→ 存储整型123400543.2→543.20→54320-0.12→-12。scale 直接决定整数分量如何从字符串的小数位换算而来。测试用例佐证pylibcudf 测试 test_string_convert_fixed_point.py 展示真实调用方式——用 PyArrow 构造数据经plc.Column.from_arrow转入并用plc.DataType.from_arrow(pa.decimal128(38, 2))指定 Decimal128(scale2)import pyarrow as pa import pylibcudf as plc typ pa.decimal128(38, 2) arr pa.array([123, 1.23, None]) got plc.strings.convert.convert_fixed_point.to_fixed_point( plc.Column.from_arrow(arr), plc.DataType.from_arrow(typ) ) # 结果等价于 arr.cast(typ)[Decimal(123.00), Decimal(1.23), None]异常行为若output_type不是定点十进制类型C 层会抛出cudf::logic_error该异常经 pxd 中的libcudf_exception_handler转换为 Python 异常。from_fixed_point将定点列格式化为字符串函数签名与参数from_fixed_point(input: Column, stream: CudaStreamLike | None None, mr: DeviceMemoryResource | None None) - Columninput必须为定点列返回新的 stringsColumn。null 条目保持 null。格式化规则C 文档convert_fixed_point.hpp明确每个值按**十进制base-10**生成字符串负数在输出字符串中带-前缀列的 scale 值决定小数点位置负 scale 会在小数点后补齐零。官方示例fp is [110, 222, 3330, -440, -1] with scale -2 s from_fixed_point(fp) s is now [1.10, 2.22, 33.30, -4.40, -0.01]注意-1在 scale-2 下被格式化为-0.01补零而3330成为33.30。测试用例佐证test_string_convert_fixed_point.pyimport decimal arr pa.array([decimal.Decimal(1.1), None]) got plc.strings.convert.convert_fixed_point.from_fixed_point( plc.Column.from_arrow(arr), ) # 期望结果[1.1, None]如果输入列不是定点十进制类型同样抛出cudf::logic_error。is_fixed_point转换前的合法性预检函数签名与参数is_fixed_point(input: Column, decimal_type: DataType | None None, stream: CudaStreamLike | None None, mr: DeviceMemoryResource | None None) - Columndecimal_type仅用于检查溢出的定点类型含 scale默认值为Decimal64pyx 中if decimal_type is None: decimal_type DataType(type_id.DECIMAL64)见 convert_fixed_point.pyx。返回布尔列逐字符串标记是否可以安全转换为定点数。判定规则C 文档convert_fixed_point.hpp列出符号位可选小数点最多只能出现一次整数部分必须能装入底层定点存储类型的尺寸范围且整数分量的大小由传入decimal_type的 scale 决定null 条目在输出中保持 null若decimal_type非定点类型抛出cudf::logic_error。官方示例s [123, -456, , 1.2.3, 17E30, 12.34, .789, -0.005] b is_fixed_point(s) b is [true, true, false, false, true, true, true, true]空串→false1.2.3两个小数点→false17E30科学计数法且按 Decimal64 判断溢出→true.789、-0.005等合法小数 →true。测试用例佐证test_string_convert_fixed_point.pyarr pa.array([123, 1.23, 1.2.3, , None]) got plc.strings.convert.convert_fixed_point.is_fixed_point( plc.Column.from_arrow(arr), ) # 期望结果[True, True, False, False, None]典型组合用法由于to_fixed_point对无效格式给出未定义行为推荐先用is_fixed_point过滤再转换import pyarrow as pa import pylibcudf as plc raw pa.array([12.34, bad, -0.005, , 987654321.1]) col plc.Column.from_arrow(raw) ok plc.strings.convert.convert_fixed_point.is_fixed_point(col) valid_mask ok.to_arrow() # 布尔掩码用于筛选合法行 typ plc.DataType.from_arrow(pa.decimal128(38, 2)) fp plc.strings.convert.convert_fixed_point.to_fixed_point(col, typ)底层实现与调用链完整调用链为Python API (pyx) → Cython pxd 声明 → libcudf C (cpp/include/cudf/strings/convert/convert_fixed_point.hpp) → strings_column_view/column_view 输入 → CUDA 内核解析 → unique_ptrcolumn 结果值得注意的实现细节内存与流管理pyx 层把 Python 侧stream/mr参数统一解析为 CUDA 流对象与 RMMDeviceMemoryResource释放 GILwith nogil后在设备端执行保证并发与异步安全convert_fixed_point.pyx。异常桥接pxd 中每个函数都以except libcudf_exception_handler声明将 C 的logic_error等异常翻译为 Python 异常convert_fixed_point.pxd。默认参数对称C 层to_fixed_point的默认流为cudf::get_default_stream()、默认内存资源为cudf::get_current_device_resource_ref()Python 层则通过_get_stream/_get_memory_resource与之对齐。scale 语义贯穿双向转换to_fixed_point用output_type的 scale 换算整数分量from_fixed_point用列自身的 scale 摆放小数点——因此“读入再写出”时scale 一致即可无损还原字符串。使用注意事项小结to_fixed_point前务必用is_fixed_point预检规避未定义行为溢出不会在转换时报错若业务数据可能超界请按实际取值范围选择Decimal32/64/128并配合 scale 合理设置decimal_type科学计数法如17E30会被is_fixed_point视为合法在未溢出前提下但to_fixed_point的格式约定只含数字与小数点两者判定粒度不同需结合实际场景验证null 语义在三个 API 中保持一致输入 null → 输出 null。延伸阅读同子包其他转换 APIconvert/index.rstPython 封装源码convert_fixed_point.pyxCython 类型声明convert_fixed_point.pxdC 实现头文件含全部格式约定与示例convert_fixed_point.hpp单元测试test_string_convert_fixed_point.py赞分享数据分析数据工程机器学习【免费下载链接】cudfcuDF - GPU DataFrame Library项目地址https://gitcode.com/gh_mirrors/cu/cudf点击查看免费下载相关推荐cuDF 字符串与 Duration 互转全解析pylibcudf 的 convert_durations 模块实战指南cuDF 字符串与 Duration 互转全解析pylibcudf 的 convert_durations 模块实战指南 导读 本文聚焦 cuDFGPU D数据分析数据工程机器学习cuDF pylibcudf 字符串与时间戳互转指南convert_datetime 模块的 to_timestamps / from_timestamps / is_timestamp 全解析cuDF pylibcudf 字符串与时间戳互转指南convert_datetime 模块的 to_timestamps / from_timestamps数据分析数据工程机器学习pylibcudf 字符串与整数互转convert_integers 模块完整指南pylibcudf 字符串与整数互转convert_integers 模块完整指南 convert_integers 是 pylibcudf 字符串转换子模块数据分析数据工程机器学习创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考