pandas 2.3.2 发布详解:StringDtype 系列修复与 pandas 3.0 默认字符串类型的最后铺垫
pandas 2.3.2 发布详解StringDtype 系列修复与 pandas 3.0 默认字符串类型的最后铺垫【免费下载链接】pandasFlexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more项目地址: https://gitcode.com/gh_mirrors/pa/pandas本篇文章基于 doc/source/whatsnew/v2.3.2.rst 官方发布说明展开系统梳理 pandas 2.3.22025 年 8 月 21 日发布中针对StringDtype的四项关键缺陷修复。文中不仅复现每项修复的触发场景与正确用法还深入对应源码与测试用例帮助读者理解StringDtype的底层实现细节并为即将在 pandas 3.0 中成为默认字符串 dtype 的迁移工作做好准备。版本概览一次面向字符串 dtype 的集中修补pandas 2.3.2 是 2.3.x 系列的一个补丁版本绝大多数改动都围绕StringDtype展开。正如发布说明开篇所指出的Most changes in this release are related toStringDtypewhich will become the default string dtype in pandas 3.0.也就是说当前仓库中的字符串objectdtype 在未来版本将被StringDtype支持python与pyarrow两种存储后端取代。这一版本修复了以下四类问题DataFrame.to_json(orienttable)对StringDtype列未正确使用string类型issue 61889bool dtype 对象与StringDtype对象之间的布尔运算|、、^行为不一致issue 60234Arrow 存储后端的Series.str.match、Series.str.fullmatch、Series.str.contains在传入已编译正则compiled regex时出错issue 61964、61942Series.replace/DataFrame.replace在字符串 dtype 存在缺失值时替换结果不一致issue 56599。StringDtype类定义在 pandas/core/arrays/string_.py 中属于StorageExtensionDtype可通过pd.StringDtype()或pd.StringDtype(storagepython)创建其默认缺失值标记为pd.NAna_valueNA。修复一to_json(orienttable)的 JSON Table Schema 类型修正问题背景pandas 的to_json(orienttable)输出包含schema与data两部分schema遵循 JSON Table Schema 规范其中每个字段带有一个type属性。在 2.3.2 之前StringDtype列在此处被错误归类导致使用该 schema 重建 DataFrame 时类型信息失真。源码佐证类型映射的核心逻辑位于 pandas/io/json/_table_schema.py 的pandas_type_to_json_field函数int64 - integer float64 - number bool - boolean datetime64[ns] - datetime timedelta64[ns] - duration object - str categorical - any其中字符串类型统一通过is_string_dtype(x)判定并映射为string第 96-97 行。本次修复正是保证StringDtype列走通这一分支而非被归入any或其他类型。build_table_schema与parse_table_schema分别负责 schema 的构建与回读见 pandas/io/json/_table_schema.py、pandas/io/json/_table_schema.py。验证方式import pandas as pd df pd.DataFrame( {col 1: [a, b], col 2: [c, d]}, indexpd.Index([row 1, row 2], dtypestring), ) df df.astype(string) # 使用 StringDtype out df.to_json(orienttable) print(out)修复后schema 中StringDtype列的type正确为string形如{schema:{fields:[ {name:index,type:string,extDtype:str}, {name:col 1,type:string,extDtype:str}, {name:col 2,type:string,extDtype:str}], primaryKey:[index],pandas_version:1.4.0}, data:[{index:row 1,col 1:a,col 2:b}, {index:row 2,col 1:c,col 2:d}]}该输出格式同样可见于 pandas/io/json/_json.py 的to_jsondocstring 示例。需要注意orienttable模式下不允许同时传入dtypeValueError: cannot pass both dtype and orienttable也不支持convert_axes参数见 pandas/io/json/_json.py。Series同样支持orienttable但 MultiIndex 列暂不支持pandas/io/json/_json.py。修复二bool 与StringDtype混合布尔运算的弃用警告问题背景当布尔类型对象位于运算符左侧、StringDtype对象位于右侧时如bool_series | string_seriespandas 2.3.2 之前的行为存在歧义。自 2.3.2 起此类操作会把字符串一侧转换为布尔值并发出弃用警告为 pandas 3.0 的强制类型规则铺路。源码佐证实现位于 pandas/core/arrays/string_.py 的BaseStringArray._logical_method当操作符为反射运算ror_、rand_、rxor且对侧是dtype bool的np.ndarray时对应 GH#60234 的向后兼容场景先发出Pandas4Warning提示op operations between boolean dtype and dtype are deprecated and will raise in a future version并建议显式将字符串转换为布尔 dtype 后再运算随后通过op(other, self.astype(bool))把字符串数组强制转换为布尔后完成运算。源码注释明确写道# GH#60234 backward compatibility for the move to StringDtype in 3.0推荐写法与其依赖隐式转换不如显式转换以消除警告import pandas as pd s_bool pd.Series([True, False, True], dtypebool) s_str pd.Series([True, False, True], dtypestring) # 2.3.2 起会发出 Pandas4Warning并自动将右侧转为布尔 result s_bool | s_str # 推荐显式转换 result s_bool | s_str.astype(bool)相关行为测试可参见 pandas/tests/series/test_logical_ops.py 与 pandas/tests/arithmetic/test_string.py其中引用了 issue 60234。修复三Arrow 存储后端对已编译正则的支持问题背景StringDtype(storagepyarrow)使用 Apache Arrow 的字符串计算内核执行str访问器方法其正则语义基于 RE2。此前将re.compile(...)编译后的正则对象传入Series.str.match、Series.str.fullmatch、Series.str.contains时会出现行为异常或报错。2.3.2 修复了这三个方法对 compiled regex 的处理对应 issue 61964、61942。源码佐证核心实现在 pandas/core/arrays/string_arrow.py_preprocess_re_pattern第 425-435 行负责通过_unwrap_re_pattern解包已编译正则并将 Python 风格的行尾锚点\Z改写为 RE2 支持的\z_unescaped_end_anchor.sub(r\1\\z, pattern)_str_contains/_str_match/_str_fullmatch第 437-491 行采用统一的降级策略当传入flags、模式本身携带 flags_is_re_pattern_with_flags、或包含 RE2 不支持的语法_has_unsupported_regex时自动回退到父类的 object 路径实现否则走 Arrow 原生内核并应用_preprocess_re_pattern预处理。这套逻辑保证 compiled regex 与字符串模式两种传法结果一致。验证方式测试用例集中在 pandas/tests/strings/test_find_replace.pytest_contains_compiled_regex第 269-302 行GH#61942pd.Series([foo,bar,Baz]).str.contains(re.compile(ba.))应返回[False, True, False]编译模式中已含re.IGNORECASE时同样生效而已编译模式 额外 flags会抛出ValueError: cannot process flags argument with a compiled patterntest_match_compiled_regex第 1201-1232 行str.match(re.compile(ab))返回[True, False, True, False]若case/flags与编译模式冲突则抛ValueError提示 Cannot both specify case and pass a compiled regexp object with conflicting case-sensitivity一致时则允许同时传入test_fullmatch_compiled_regex第 1536 行起与test_contains_compiled_regex_flags第 305 行起进一步覆盖fullmatch及其他 flags如re.MULTILINE的语义保持。使用建议import re import pandas as pd ser pd.Series([ab, AB, abc, ABC], dtypestring[pyarrow]) pat re.compile(ab, flagsre.IGNORECASE) print(ser.str.match(pat)) # 编译模式自带的 IGNORECASE 生效 print(ser.str.contains(pat)) print(ser.str.fullmatch(pat))请勿在同一调用中同时传入编译模式与冲突的case/flags参数否则会抛出ValueError。修复四replace在存在缺失值时的一致性问题背景对于字符串 dtype 的 Series/DataFrame当数据中含有缺失值如pd.NA、空字符串时replace对匹配值替换的处理前后不一致issue 56599部分匹配被遗漏或结果不稳定。源码佐证回归测试位于 pandas/tests/series/methods/test_replace.py 的test_pandas_replace_na标注 GH#56599ser pd.Series( [AA, BB, CC, DD, EE, , pd.NA, AA], dtypestring, ) regex_mapping { AA: CC, BB: CC, EE: CC, CC: CC-REPL, } result ser.replace(regex_mapping, regexTrue) exp pd.Series( [CC, CC, CC-REPL, DD, CC, , pd.NA, CC], dtypestring, )修复后正则映射中的每一条规则都按预期生效AA、BB、EE均被替换为CC而CC本身又被后续规则替换为CC-REPL规则按字典顺序依次应用缺失值pd.NA与空字符串则保持不变且结果 dtype 仍为string。该文件还覆盖了其他类型bool、int64、Int64、float64、Float64等的replace行为第 555 行起的参数化测试可据此确认修复未影响非字符串 dtype 的替换语义。从 2.3.2 到 pandas 3.0 的迁移要点综合本版本的四项修复可以提炼出面向未来版本的几条实践建议类型映射规范化凡涉及 JSON 导出的场景尤其orienttableStringDtype现在与object字符串一样映射为 schema 的string类型可放心用于跨系统数据交换与回读布尔运算显式化避免将 bool 数组与字符串数组直接做|、、^运算改用astype(bool)显式转换消除Pandas4Warning并为 3.0 的严格类型规则做好准备正则参数收敛使用str.match/str.fullmatch/str.contains时优先在编译模式中固化 flags不要再同时传case/flagsArrow 后端会自动完成\Z→\z等 RE2 兼容改写缺失值语义确认replace对pd.NA/NaN保留原值正则映射按规则顺序依次替换结果保持字符串 dtype——这在数据清洗管道中是可依赖的稳定行为。贡献者与发布信息2.3.2 的完整提交历史收录于v2.3.1..v2.3.2区间由 doc/source/whatsnew/v2.3.2.rst 中的.. contributors:: v2.3.1..v2.3.2指令自动生成完整变更日志可在 doc/source/whatsnew/index.rst 的 release notes 中查阅。上述修复源码均位于当前仓库的 pandas/core/arrays/string_.py、pandas/core/arrays/string_arrow.py、pandas/io/json/_table_schema.py 等文件配套测试可在 pandas/tests/strings/test_find_replace.py、pandas/tests/series/methods/test_replace.py、pandas/tests/series/test_logical_ops.py 中复现验证。【免费下载链接】pandasFlexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more项目地址: https://gitcode.com/gh_mirrors/pa/pandas创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考