pandas hasnan() on Series gives "TypeError: 'numpy.bool_' object is not callable

pandas hasnan() on Series gives "TypeError: 'numpy.bool_' object is not callable

在我的测试中,我有一个方法check_nulls来检查特定列的空值

def check_nulls(self, name, column_list):
        """ Ensure that the table given has no nulls in any of the listed columns
            @param name the name of the table to check
            @param column_list the columns to check for nulls
        """
        df =  util.TABLES.load_table(name,
                                            config.get_folder("TRANSFORMED_FOLDER"),
                                     sep='|')
        #print df
        for column in column_list:
            print df[column].dtypes
            print df[column]
            self.assertFalse(df[column].dtypes != "int32" 
                                and df[column].dtypes != "int64" 
                                and df[column].hasnans(),
                             '{0} in {1} contains null values'\
                             .format(column, name))

错误发生在 df[column].hasnans() 它在某些表上给我一个 typeError。

TypeError: 'numpy.bool_' object is not callable

起初我认为这是一个问题,因为 int 列没有真正的 null,因为如果它们有 null,它们将被转换为 float 列,我添加了检查豁免,但我现在 运行 到 dtype 为 "object" 的列中,这也给我错误。

如何正确检查数据框列中的空值?我检查了 df[column] 的类型,它实际上是一个 Series.

哇,我觉得这很傻,但我会留下这个问题,以防其他人犯同样的愚蠢错误。

hasnans 是一个布尔值而不是函数解决方案是不要尝试调用它

df[列].hasnans 不是 df[列].hasnans()

hasnans是一个简单的布尔值,不是方法,所以不能调用。

但是,我认为这不是确定 Series 是否包含 nan 的可靠方法。如果您修改系列,它不会更新:

>>> x = pandas.Series([1, 2, 3])
>>> x.hasnans
False
>>> x[1] = np.nan
>>> x
0     1
1   NaN
2     3
dtype: float64
>>> x.hasnans
False

要检查系列是否包含 nans,请使用 if mySeries.isnull().any()