What does float' object has no attribute 'replace' when I try locale.atof in Pandas?

Well, I don't know how "smart" this is, but I "fixed" it like this, at least for the time being:

df.idh = df.idh.astype(str).apply(locale.atof)

Please, do let me know the smart answer to this.


The problem has to do with null values. Replace cannot work on numpy nulls. Your solution doesn't return an error because you convert np.NaN (nulls) into 'nan' and replace can work on the string 'nan'. The problem is though that you now have 'nan' in your column instead of np.NaN. So if you run:

df[df.idh.isnull()]

It would return 0 rows even though you do have nulls in your data. The following code keeps the np.NaN while running a replace statement on that column.

def replace_percent(x):
    try: 
        return x.replace('%', '')
    except AttributeError:
        return np.NaN

df_not.secularism = df_not.secularism.map(replace_percent)

Tags:

Python

Pandas