简体   繁体   中英

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

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.

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.

Pandas.to_numeric is only available for version 0.17 and higher. You can use DataFrame.convert_objects with convert_numeric=True argument instead, errors are automatically coerced.

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM