Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to force pandas not to convert data type when using DataFrame.replace

Tags:

python

pandas

Here's a working example:

df = pd.DataFrame({'A': [-39882300000000000000]}, dtype='object')

df.replace({',': '.'}) raises an OverflowError because somewhere in the code the convert flag is set to True. I am not sure but it is probably because pandas is inferring that it only contain numbers.

I read the data from an Excel workbook and I want to prevent this conversion when using df.replace. Is there a way to do so?

like image 231
ayhan Avatar asked Aug 21 '17 21:08

ayhan


People also ask

Does DataFrame preserve order?

Pandas. DataFrame doesn't preserve the column order when converting from a DataFrames.

What does errors =' coerce meaning?

errors{'ignore', 'raise', 'coerce'}, default 'raise' If 'raise', then invalid parsing will raise an exception. If 'coerce', then invalid parsing will be set as NaN. If 'ignore', then invalid parsing will return the input.

Can pandas series hold different data types?

In the same way you can't attach a specific data type to list , even if all elements are of the same type, a Pandas object series contains pointers to any number of types.


1 Answers

df.update(df.blocks['object'].astype(str).replace({',': '.'}))
like image 199
piRSquared Avatar answered Oct 23 '22 16:10

piRSquared