AttributeError:'模块'对象没有属性' to_numeric'在熊猫

时间:2017-06-05 06:49:13

标签: python pandas

以下代码在Pandas版本为0.16.2的服务器上抛出AttributeError,而在我的机器上运行良好,版本为0.20。

df = pandas.read_csv('filename', header = None, error_bad_lines = False, warn_bad_lines =True,quoting=csv.QUOTE_NONE)

df = df.drop(df[pandas.to_numeric(df[599], errors='coerce').isnull()].index)

错误消息如下:

Traceback (most recent call last):
  File "train_model.py", line 11, in <module>
    df = df.drop(df[pandas.to_numeric(df[599], errors='coerce').isnull()].index)
AttributeError: 'module' object has no attribute 'to_numeric'

有没有办法在0.16.2版本中避免此错误?无法更新服务器。

2 个答案:

答案 0 :(得分:2)

Pandas.to_numeric仅适用于0.17及更高版本。您可以使用DataFrame.convert_objects代替convert_numeric=True参数,自动强制执行错误。

df = df.drop(df[df[599].convert_objects(convert_numeric=True).isnull()].index)

答案 1 :(得分:2)

如果您在pandas文档中注意到版本0.17中的新功能, 你应该注意到

  

pd.to_numeric是一个将字符串强制转换为数字的新函数(可能带有强制)(GH11133)

因此,pandas 0.16没有函数pd.to_numeric。但是,您可以使用此功能来实现相同的目的。

df = df.drop(df[df[599].astype(float).isnull()].index)