decimal option in pd.read_excel() deprecated?

Viewed 3979

Until recently I used

pd.read_excel("afile.xslx", decimal=',')

with a decimal option, which allows it to parse European dates very easily.

I updated pandas to version 1.1.3 today and it doesn't accept the option "decimal = " anymore. Is this option deprecated and if so, what is a good way to import numbers that are stored as text in excel with "," as decimal point, for example "11,3" ?

2 Answers

I don't think that read_excel ever accepted a decimal argument. This github issue asks for it (an ongoing issue that was opened in 2016 and is still active, as of October 2020).

I think you are confused with read_csv that still accepts decimal as of pandas 1.1.3.

Anyhow, with read_excel you can use the converters argument. From the docs:

converters: dict, default None. Dict of functions for converting values in certain columns. Keys can either be integers or column labels, values are functions that take one input argument, the Excel cell content, and return the transformed content.

def decimal_converter(value):
    try:
        return float(value.replace(',', '.'))
    except ValueError:
        return value

pd.read_excel("afile.xslx", converters={'column_name': decimal_converter})

Another solution that works for me is:

bd_df['col_1'] = bd_df['col_1'].str.replace(",", ".").astype(float)

Related