AttributeError: 'module' 对象在 Pandas 中没有属性 'to_numeric'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44363192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:43:59  来源:igfitidea点击:

AttributeError: 'module' object has no attribute 'to_numeric' in Pandas

pythonpandas

提问by silent_dev

The following code is throwing AttributeError on a server where the version of Pandas is 0.16.2 whereas it runs fine on my machine where version is 0.20.

以下代码在 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)

The error message is the following:

错误消息如下:

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'

Is there a way to avoid this error in 0.16.2 version? The update to the server is not possible.

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

回答by umutto

Pandas.to_numeric is only available for version 0.17 and higher. You can use DataFrame.convert_objectswith convert_numeric=Trueargument instead, errors are automatically coerced.

Pandas.to_numeric 仅适用于 0.17 及更高版本。您可以使用带参数的DataFrame.convert_objectsconvert_numeric=True来代替,错误会被自动强制转换。

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

回答by crazyglasses

If you notice in the pandas documentation of what's new in version 0.17, you shall notice

如果您在 pandas 文档中注意到 0.17 版中的新功能,您会注意到

pd.to_numeric is a new function to coerce strings to numbers (possibly with coercion) (GH11133)

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

Hence, pandas 0.16 does not have the function pd.to_numeric. However you can use this function to achieve the same purpose.

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

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