python - 为什么 pandas.Series.replace() 在版本之间有不同的功能?
问题描述
问题:pandas.Series.replace()
成功将值替换bool
为版本中的浮点数0.24.2
。在版本中运行相同的代码行1.1.3
会产生不同的结果。
示例代码:
使用0.24.2
:
import pandas
pd.__version__ # 0.24.2
a = pd.Series([True, False, False, True])
a
0 True
1 False
2 False
3 True
dtype: bool
a.replace(True, 1.2).replace(False, 1)
0 1.2
1 1.0
2 1.0
3 1.2
dtype: float64
使用1.1.3
:
import pandas
pd.__version__ # 1.1.3
a = pd.Series([True, False, False, True])
a
0 True
1 False
2 False
3 True
dtype: bool
a.replace(True, 1.2).replace(False, 1)
0 1.2
1 0.0
2 0.0
3 1.2
dtype: float64
我也尝试了与我在这里找到的类似的语法:https ://github.com/pandas-dev/pandas/pull/6339 ,但这会产生不同的结果。
a.replace({True: 1.2, False: 1})
0 1
1 1
2 1
3 1
dtype: float64
我可以使用下面的代码成功更改它,但没有发现任何有关功能更改的信息。额外的.astype(int)
似乎没有必要。
a.astype(int).replace({1: 1.2, 0: 1})
0 1.2
1 1.0
2 1.0
3 1.2
dtype: float64
解决方案
主要版本(0.xx 与 1.xx)预计不会向后兼容。事实上,即使是次要版本和补丁版本,开发版本 (0.xx) 也不保证向后兼容。
如果将任何向后不兼容的更改引入公共 API,则必须增加主要版本 X (Xyz | X > 0)。它还可能包括次要和补丁级别更改。当主要版本增加时,补丁和次要版本必须重置为 0。
推荐阅读
- php - 如何仅在 PHP 中将数据库行转换为 JSON 值字符串?
- php - 错误:stdClass 类的对象无法转换为 int
- java - 具有多个配置的 Spring Boot 测试
- javascript - 如何在 ReactJS 中更改按钮文本
- elasticsearch - 从弹性搜索中过滤重复的 Like url
- spring - 使用 JPA 查询数据库以获取空值
- python - 如何强制使产生子进程的进程超时
- python - 循环和浮点数
- smartcard - Java 卡本地固定大小数组可变内存存储位置
- python - Python如何查找一个数字是否在列表中但不是列表的最后一个数字?