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

ayhan 08/21/2017. 2 answers, 200 views
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?

1 piRSquared 08/21/2017
IIUC convert it to str. df.astype(str).replace({',': '.'})

2 Answers

piRSquared 08/21/2017.
df.update(df.blocks['object'].astype(str).replace({',': '.'}))
1 MaxU 08/21/2017
df.blocks['object'] - wow! This is cool!
1 piRSquared 08/21/2017
You remember I brought it up a while ago... this is the first time I've used it to answer a question (-:
1 MaxU 08/21/2017
@ayhan, there is a documented method:…
1 piRSquared 08/21/2017
@ayhan neither does get_value or set_value but jeff told me that it wasn't intended as public api. So, still not sure
1 piRSquared 08/22/2017
@Jeff what specifically? blocks, as_blocks, or get/set_value?

MaxU 08/21/2017.

How about this:

In [25]: df.loc[:, df.dtypes=='object'] = \
             df.select_dtypes(['object']).astype(str).replace({',': '.'})

This will apply .replace only to columns of a string (object) dtype - Download Hi-Res Songs

1 (G)I-DLE


(G)I-DLE. 2018. Writer: Riot Music Team;Harloe.
2 Ariana Grande

​Thank U, Next flac

Ariana Grande. 2018. Writer: Crazy Mike;Scootie;Victoria Monét;Tayla Parx;TBHits;Ariana Grande.
3 Imagine Dragons

Bad Liar flac

Imagine Dragons. 2018. Writer: Jorgen Odegard;Daniel Platzman;Ben McKee;Wayne Sermon;Aja Volkman;Dan Reynolds.
4 Clean Bandit

Baby flac

Clean Bandit. 2018. Writer: Jack Patterson;Kamille;Jason Evigan;Matthew Knott;Marina;Luis Fonsi.
5 BlackPink

Kiss And Make Up flac

BlackPink. 2018. Writer: Soke;Kny Factory;Billboard;Chelcee Grimes;Teddy Park;Marc Vincent;Dua Lipa.
6 Kelly Clarkson

Never Enough flac

Kelly Clarkson. 2018. Writer: Benj Pasek;Justin Paul.
7 Backstreet Boys

Chances flac

Backstreet Boys. 2018.

Waste It On Me flac

BTS. 2018. Writer: Steve Aoki;Jeff Halavacs;Ryan Ogren;Michael Gazzo;Nate Cyphert;Sean Foreman;RM.
9 Bradley Cooper

Always Remember Us This Way flac

Bradley Cooper. 2018. Writer: Lady Gaga;Dave Cobb.
10 Mako

Rise flac

Mako. 2018. Writer: Riot Music Team;Mako;Justin Tranter.
11 Halsey

Without Me flac

Halsey. 2018. Writer: Halsey;Delacey;Louis Bell;Amy Allen;Justin Timberlake;Timbaland;Scott Storch.
12 Lady Gaga

I'll Never Love Again flac

Lady Gaga. 2018. Writer: Benjamin Rice;Lady Gaga.
13 Little Mix

Told You So flac

Little Mix. 2018. Writer: Eyelar;MNEK;Raye.
14 Haley Reinhart

Something Strange flac

Haley Reinhart. 2018.
15 Little Mix

Woman Like Me flac

Little Mix. 2018. Writer: Nicki Minaj;Steve Mac;Ed Sheeran;Jess Glynne.
16 Bradley Cooper

Shallow flac

Bradley Cooper. 2018. Writer: Andrew Wyatt;Anthony Rossomando;Mark Ronson;Lady Gaga.
17 Diplo

Close To Me flac

Diplo. 2018. Writer: Ellie Goulding;Savan Kotecha;Peter Svensson;Ilya;Swae Lee;Diplo.
18 Imagine Dragons

Machine flac

Imagine Dragons. 2018. Writer: Wayne Sermon;Daniel Platzman;Dan Reynolds;Ben McKee;Alex Da Kid.

Magic flac

KSHMR. 2018.
20 Call Me Karizma

God Damnit flac

Call Me Karizma. 2018. Writer: Illenium;Call Me Karizma.

Related questions

Hot questions


Popular Tags