“模块"对象没有属性“isna" [英] 'module' object has no attribute 'isna'
问题描述
我有一个名为 batch_df
的 df,我在 df 中添加了as_percentage".如果 hours 为 Nan,则将 as_percentage 设置为 0.代码在 PyCharm 中运行良好,但当我在终端上运行时,出现此错误:请帮忙!
I have a df called batch_df
and I added 'as_percentage' into the df. If hours is Nan, then set as_percentage as 0. The code ran fine in PyCharm but when I ran it on the terminal, I got this error: Please help!
回溯(最近一次调用最后一次):
Traceback (most recent call last):
文件/Users/vic_ong/dev/resource-management/generateBatch.py",第 166 行,在main()
File "/Users/vic_ong/dev/resource-management/generateBatch.py", line 166, in main()
文件/Users/vic_ong/dev/resource-management/generateBatch.py",第 51 行,在主目录中
File "/Users/vic_ong/dev/resource-management/generateBatch.py", line 51, in main
batch_df.loc[pd.isna(batch_df['hours']) == False,'as_percentage'] = 0AttributeError: 'module' 对象没有属性 'isna'
batch_df.loc[pd.isna(batch_df['hours']) == False, 'as_percentage'] = 0 AttributeError: 'module' object has no attribute 'isna'
编辑——我的代码如下所示:
Edit -- My code looks like this:
#!/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 示例:
batch_df sample:
name hours as_percentage维南1vic1南1
推荐答案
isnull
的 isna
别名仅在 0.21 版 中添加(0.22 是截至 2018 年 1 月的最新版本);在 0.20 及更早版本,它不存在;您必须改用旧的(并且仍然受支持)isnull
名称.更新您的 pandas
安装,或使用旧名称.
The isna
alias for isnull
was only added in version 0.21 (0.22 is the latest release as of Jan 2018); on 0.20 and earlier, it doesn't exist; you have to use the older (and still supported) isnull
name instead. Either update your pandas
install, or use the older name.
这篇关于“模块"对象没有属性“isna"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!