Skip to content
Advertisement

Is there a more efficient way to find and downgrade int64 columns with to_numeric() in Python Pandas?

tl;dr: Need help cleaning up my downcast_int(df) function below.

Hello, I’m trying to write my own downcasting functions to save memory usage. I am curious about alternatives to my (frankly, quite messy, but functioning) code, to make it more readable – and, perhaps, faster.

The downcasting function directly modifies my dataframe, something I am not sure I should be doing.

Any help is appreciated.

Example df

JavaScript
    first   second  third   fourth  fifth
0   1000    -30     some    4.5     -6
1   200000  -40000  string  6.1     -8

df.info()

JavaScript

Downcasting function

JavaScript

df.info() after downcasting

JavaScript

Advertisement

Answer

Just apply to_numeric() twice. Once to get to min signed, then a second time to reduce the unsigned.

JavaScript

Same output as your method:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement