
在python的64位环境中,我们通常期望pandas在处理整数时默认使用int64类型,以充分利用64位系统的内存地址空间和计算能力。然而,实际观察到的行为可能有所不同:
显式指定dtype=int的行为: 当通过 pd.Series([1,2,3], dtype=int) 显式指定数据类型为 int 时,Pandas 可能会将其解释为 int32。这主要是因为 dtype=int 在 Pandas 内部映射到 NumPy 的 np.int_ 类型,而 np.int_ 在某些平台上(例如Windows的64位系统)为了ABI兼容性或内存效率,可能默认指向 int32。这意味着,即使运行在64位Python上,除非数值超出int32范围,否则Pandas可能倾向于使用更小的 int32 类型。
import pandas as pd
import platform
import sys
# 验证Python环境为64位
assert platform.architecture()[0] == "64bit"
assert sys.maxsize > 2**32
print(f"Python环境架构: {platform.architecture()[0]}")
print(f"sys.maxsize: {sys.maxsize}")
# 显式指定dtype=int
s_int_explicit = pd.Series([1, 2, 3], dtype=int)
print(f"pd.Series([1,2,3], dtype=int).dtype: {s_int_explicit.dtype}")输出通常会显示 int32。
自动推断数据类型的行为: 当创建 Series 或 DataFrame 时不显式指定 dtype,Pandas 会根据数据内容自动推断最合适的数据类型。在64位Python环境中,对于整数数据,Pandas 往往会推断为 int64,因为它是一个更通用的选择,能够处理更大的数值范围。
# 不显式指定dtype
s_int_inferred = pd.Series([1, 2, 3])
print(f"pd.Series([1,2,3]).dtype: {s_int_inferred.dtype}")输出通常会显示 int64。
这种差异表明,dtype=int 并非总是等同于 int64,它更多地是一个泛型整数类型指示符,其具体位宽可能受环境和Pandas内部实现细节的影响。
上述数据类型默认行为的差异,在进行数据验证和测试时会带来问题,特别是当使用 pd.testing.assert_frame_equal 等严格比较函数时。assert_frame_equal 默认会检查数据框的每一个属性,包括数据类型(dtype),如果两个数据框在数值上完全相同,但一个列是 int32 而另一个是 int64,它就会抛出 AssertionError。
例如:
import pandas as pd
df_int32 = pd.DataFrame({'Int': [1, 2, 3]}, dtype='int32')
df_int64 = pd.DataFrame({'Int': [1, 2, 3]}, dtype='int64')
try:
pd.testing.assert_frame_equal(df_int32, df_int64)
print("断言成功:数据框等价")
except AssertionError as err:
print(f"断言失败:{err}")上述代码会输出断言失败信息,指出 dtype 属性不同。
虽然 pd.testing.assert_frame_equal 提供了 check_dtype=False 参数来忽略数据类型检查,但这通常不是一个理想的解决方案,因为它可能掩盖其他重要的类型不匹配问题,降低测试的严谨性。
为了在测试中既能保证数据类型的合理性,又能兼容 int32 和 int64 这种“等效”的数值类型差异,我们可以实现一个自定义的断言函数。这个函数的核心思想是:在比较之前,如果两个数据框的对应列都是整数类型或都是浮点数类型,则将其中一列的数据类型统一到另一列。
以下是 assert_frame_equiv 函数的实现:
import pandas as pd
import numpy as np
def assert_frame_equiv(left: pd.DataFrame, right: pd.DataFrame) -> None:
"""
比较两个DataFrame是否等效,并在比较前将等效的数值数据类型统一。
如果DataFrame的列名或数据不匹配,将抛出AssertionError。
"""
# 首先,检查列名是否相同
pd.testing.assert_index_equal(left.columns, right.columns, check_order=False)
# 复制DataFrame以避免修改原始数据
left_copy = left.copy()
right_copy = right.copy()
# 遍历所有列,对等效类型进行统一
for col_name in left_copy.columns:
lcol = left_copy[col_name]
rcol = right_copy[col_name]
# 检查是否都是整数类型或都是浮点数类型
is_integer_equiv = pd.api.types.is_integer_dtype(lcol) and pd.api.types.is_integer_dtype(rcol)
is_float_equiv = pd.api.types.is_float_dtype(lcol) and pd.api.types.is_float_dtype(rcol)
if is_integer_equiv or is_float_equiv:
# 如果是等效的数值类型,则将左侧列的数据类型统一到右侧列
# 优先选择更宽的类型,或者以right的类型为准
# 这里简单地将left转换为right的dtype
left_copy[col_name] = lcol.astype(rcol.dtype)
# 或者可以统一到一个通用类型,例如 int64 或 float64
# if lcol.dtype != rcol.dtype:
# target_dtype = np.promote_types(lcol.dtype, rcol.dtype)
# left_copy[col_name] = lcol.astype(target_dtype)
# right_copy[col_name] = rcol.astype(target_dtype)
# 进行最终的DataFrame比较,check_like=True 允许列和索引的顺序不同,但我们已经在前面检查了列名
# 默认情况下,assert_frame_equal会检查dtype
return pd.testing.assert_frame_equal(left_copy, right_copy, check_like=True)
# 示例使用
a = pd.DataFrame({'Int': [1, 2, 3], 'Float': [0.57, 0.179, 0.213]}) # 自动类型推断,通常为int64, float64
# 创建一个强制32位类型的DataFrame
b = a.copy()
b['Int'] = b['Int'].astype('int32')
b['Float'] = b['Float'].astype('float32')
# 创建一个强制64位类型的DataFrame
c = a.copy()
c['Int'] = c['Int'].astype('int64')
c['Float'] = c['Float'].astype('float64')
print("--- 使用 pd.testing.assert_frame_equal 直接比较 (预期失败) ---")
try:
pd.testing.assert_frame_equal(b, c)
print('成功')
except AssertionError as err:
print(f'失败: {err}')
print("\n--- 使用 assert_frame_equiv 比较 (预期成功) ---")
try:
assert_frame_equiv(b, c)
print('成功')
except AssertionError as err:
print(f'失败: {err}')代码解释:
通过使用 assert_frame_equiv 函数,我们能够在保持测试严谨性的同时,优雅地处理 Pandas 中 int32 和 int64 等效类型之间的差异。
通过对Pandas整型数据类型默认行为的深入理解,并采用灵活的测试策略,我们可以编写出更健壮、更具兼容性的代码和测试用例。
以上就是Pandas整型数据类型默认行为解析与测试兼容性策略的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号