【问题标题】:'module' object has no attribute 'isna'“模块”对象没有属性“isna”
【发布时间】:2019-04-28 06:07:04
【问题描述】:

我有一个名为 batch_df 的 df,我在 df 中添加了“as_percentage”。如果 hours 是 Nan,则将 as_percentage 设置为 0。代码在 PyCharm 中运行良好,但当我在终端上运行时,出现此错误:请帮助!

Traceback(最近一次调用最后一次):

文件“/Users/vic_ong/dev/resource-management/generateBatch.py​​”,第 166 行,在 主要()

文件“/Users/vic_ong/dev/resource-management/generateBatch.py​​”,第 51 行,在 main

batch_df.loc[pd.isna(batch_df['hours']) == False, 'as_percentage'] = 0 AttributeError: 'module' 对象没有属性 'isna'

编辑 -- 我的代码如下所示:

#!/usr/bin/env python2
import lib
import pandas as pd
import datetime
import os
pd.set_option('expand_frame_repr', False)
# get directories
batchInputDir = '/Users/vic_ong/dev/resource-management/data/input/batchUpload'


def main():
# start timer
start_time = datetime.datetime.now()

# walk into every csv file in batchUpload and concatenate them into one df
list_ = []
for root, dirs, files in os.walk(batchInputDir):
    for file in files:
        if file.endswith('.csv'):
            df = pd.read_csv(os.path.join(batchInputDir, file), index_col=None, header=0)
            list_.append(df)
batch_df = pd.concat(list_)
batch_df = batch_df.dropna(how='all')
batch_df.reset_index(drop=True, inplace=True)
# clean data frame
# rename to standardized column names
batch_df.rename(columns={'Resource Name': 'name',
                         'Hours': 'hours'},
                inplace=True)
# determine if value given was a percentage
batch_df['as_percentage'] = 1
batch_df.loc[pd.isna(batch_df['hours']) == False, 'as_percentage'] = 0
print batch_df.head

if __name__ == '__main__':
main()

batch_df 样本:

name hours as_percentage vic Nan 1 vic1 Nan 1

【问题讨论】:

  • 您需要发布其余代码。 pd 是什么?从您的 sn-p 中看不出来。是你重命名的panda 模块吗?
  • 更新你的熊猫。它可能已经过时了。
  • @robobrobro 是的,这是我重命名的 pandas 模块。我刚刚添加了我的其余代码。
  • 我不认识pandas,但你的意思是打电话给pd.isnan吗?

标签: python python-2.7


【解决方案1】:

The isna alias for isnull was only added in version 0.21(0.22 是截至 2018 年 1 月的最新版本);在 0.20 及更早的版本中,它不存在;您必须改用旧的(并且仍然受支持)isnull 名称。更新您的 pandas 安装,或使用旧名称。

【讨论】:

  • 谢谢!这是因为我的 pandas 模块已经过时了。我更新熊猫后它工作得很好:)
  • 0.21 不再是最新版本了; 0.22 came out a few weeks ago.
  • @user2357112:啊,不小心直接跳到以前的版本链接。修复。
【解决方案2】:

pandas.isna = pandas.isnull 为我解决了这个问题

【讨论】:

  • 这太棒了。就我而言,Seaborn boxplot 可能由于版本不匹配而引发此错误。在我们修复根本原因之前,这提供了一个快速的解决方法
猜你喜欢
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 2017-03-29
  • 2013-02-13
  • 2010-11-18
  • 2019-01-03
  • 2017-05-19
相关资源
最近更新 更多