Here's a simple function that will downcast floats into the smallest possible integer type that doesn't lose any information. For examples,
100.0 can be converted from float to integer, but 99.9 can't (without losing information to rounding or truncation)
Additionally, 1.0 can be downcast all the way to int8
without losing information, but the smallest integer type for 100_000.0 is int32
Code examples:
import numpy as np
import pandas as pd
def float_to_int( s ):
if ( s.astype(np.int64) == s ).all():
return pd.to_numeric( s, downcast='integer' )
else:
return s
# small integers are downcast into 8-bit integers
float_to_int( np.array([1.0,2.0]) )
Out[1]:array([1, 2], dtype=int8)
# larger integers are downcast into larger integer types
float_to_int( np.array([100_000.,200_000.]) )
Out[2]: array([100000, 200000], dtype=int32)
# if there are values to the right of the decimal
# point, no conversion is made
float_to_int( np.array([1.1,2.2]) )
Out[3]: array([ 1.1, 2.2])