pandas rounding when converting float to integer

You are right, astype(int) does a conversion toward zero:

‘integer’ or ‘signed’: smallest signed int dtype

from pandas.to_numeric documentation (which is linked from astype() for numeric conversions).

If you want to round, you need to do a float round, and then convert to int:

df.round(0).astype(int)

Use other rounding functions, according your needs.


the output is always a bit random as the ‘real’ value of an integer can be slightly above or below the wanted value

Floats are able to represent whole numbers, making a conversion after round(0) lossless and non-risky, check here for details.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)