pandas 等于神秘的行为 [英] pandas equals mysterious behavior

查看:102
本文介绍了 pandas 等于神秘的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些单元测试,但是当数据帧在各方面相同时,它都会失败。经过一番调查,我发现

  a.equals(a [a.columns])

是false,其中a是我手动创建的数据帧。为什么会有这样的原因?



编辑:
我发现问题与使用混合类型列表创建df相关:

  a = pd.DataFrame([['a',1],['b',2]])

即使列表混合,列的dtypes也是正确的。

解决方案

我想你可以使用 pd.util.testing.assert_frame_equal()方法:

 在[15]中:help(pd.util.testing.assert_frame_equal)
帮助函数assert_frame_equal在模块pandas.util.testing中:

assert_frame_equal(left,right,check_dtype = True,check_index_type ='equiv',check_column_type ='equiv',check_frame_type = True,check_less_precise
False,check_names = True,by_blocks = False,check_exact = False ,check_datetimelike_compat = False,check_like = False,obj ='DataFrame')
检查左右DataFrame是否相等。

参数
----------
left:DataFrame
right:DataFrame
check_dtype:bool,default True
是否检查DataFrame dtype是否相同。
check_index_type:bool / string {'equiv'},default False
是否检查Index类,dtype和inferred_type
是相同的。
check_column_type:bool / string {'equiv'},default False
是否检查列类,dtype和inferred_type
是相同的。
check_frame_type:bool,default False
是否检查DataFrame类是否相同。
check_less_precise:bool,default False
指定比较精度。仅在check_exact为False时使用。比较小数点后,
5位数(False)或3位数(True)。
check_names:bool,default True
是否检查Index names属性。
by_blocks:bool,default False
指定如何比较内部数据。如果为False,则按列进行比较。
如果为True,请按块进行比较。
check_exact:bool,default False
是否正确比较数字。
check_dateteimelike_compat:bool,default False
比较datetime-类似的,忽略dtype。
check_like:bool,default False
如果为true,那么reindex_like操作数
obj:str,默认'DataFrame'
指定正在比较的对象名称,内部用于显示相应的
断言消息

使用 check_dtype = False if你想要这个1.0 == 1,因为这种方法会在这种情况下返回False,因为不同的dtypes


I'm writing some unit tests, but it keeps failing when the dataframes are equal in every regard. After some investigating, I found that

a.equals( a[ a.columns ] )

is false, where a is the dataframe I've manually created. What reason could there be for that?

edit: I figured out that the issue is related to creating the df with a mixed type list:

a = pd.DataFrame( [['a',1],['b',2]] )

Even though the list is mixed, the dtypes for the columns are correct.

解决方案

I think you can use pd.util.testing.assert_frame_equal() method:

In [15]: help(pd.util.testing.assert_frame_equal)
Help on function assert_frame_equal in module pandas.util.testing:

assert_frame_equal(left, right, check_dtype=True, check_index_type='equiv', check_column_type='equiv', check_frame_type=True, check_less_precise
False, check_names=True, by_blocks=False, check_exact=False, check_datetimelike_compat=False, check_like=False, obj='DataFrame')
    Check that left and right DataFrame are equal.

    Parameters
    ----------
    left : DataFrame
    right : DataFrame
    check_dtype : bool, default True
        Whether to check the DataFrame dtype is identical.
    check_index_type : bool / string {'equiv'}, default False
        Whether to check the Index class, dtype and inferred_type
        are identical.
    check_column_type : bool / string {'equiv'}, default False
        Whether to check the columns class, dtype and inferred_type
        are identical.
    check_frame_type : bool, default False
        Whether to check the DataFrame class is identical.
    check_less_precise : bool, default False
        Specify comparison precision. Only used when check_exact is False.
        5 digits (False) or 3 digits (True) after decimal points are compared.
    check_names : bool, default True
        Whether to check the Index names attribute.
    by_blocks : bool, default False
        Specify how to compare internal data. If False, compare by columns.
        If True, compare by blocks.
    check_exact : bool, default False
        Whether to compare number exactly.
    check_dateteimelike_compat : bool, default False
        Compare datetime-like which is comparable ignoring dtype.
    check_like : bool, default False
        If true, then reindex_like operands
    obj : str, default 'DataFrame'
        Specify object name being compared, internally used to show appropriate
        assertion message

Use check_dtype=False if you want that 1.0 == 1, because this method will return False in this case because of different dtypes

这篇关于 pandas 等于神秘的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆