polars.Series.round#
- Series.round(decimals: int = 0, mode: RoundMode = 'half_to_even') Series[source]#
Round underlying floating point data by
decimalsdigits.- Parameters:
- decimals
Number of decimals to round by.
- mode{‘half_to_even’, ‘half_away_from_zero’, ‘to_zero’}
The rounding strategy used. A “rounded value” is a value with at most
decimalsdecimal places (e.g. integers whendecimals=0, multiples of 0.1 whendecimals=1, 0.01 whendecimals=2, and so on).Strategies that start with
half_round all values to the nearest rounded value, only using the strategy to break ties when a value falls exactly between two rounded values (e.g. 0.5 whendecimals=0, 0.05 whendecimals=1). Other rounding strategies specify explicitly which rounded value is chosen and always apply (not just for tiebreaks).- half_to_even (default)
Round to the nearest value; break ties by choosing the nearest even value. For example, 0.5 rounds to 0, 1.5 rounds to 2, 2.5 rounds to 2. Also known as “banker’s rounding”; this is the default because it tends to minimise cumulative rounding bias.
- half_away_from_zero
Round to the nearest value; break ties by rounding away from zero. For example, 0.5 rounds to 1, -0.5 rounds to -1, 2.5 rounds to 3. Also known as “commercial rounding”.
- to_zero
Always round (truncate) towards zero, discarding the fractional part beyond
decimals. For example, 0.9 rounds to 0, -0.9 rounds to 0, 1.29 rounds to 1.2 (withdecimals=1). Equivalent to thetruncate()method.
Examples
>>> s = pl.Series("a", [1.12345, 2.56789, 3.901234]) >>> s.round(2) shape: (3,) Series: 'a' [f64] [ 1.12 2.57 3.9 ]
>>> s = pl.Series([-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5]) >>> s.round(mode="half_to_even") shape: (8,) Series: '' [f64] [ -4.0 -2.0 -2.0 -0.0 0.0 2.0 2.0 4.0 ]