Search code examples
pythonroundingbinning

Rounding to the closest bin in pandas/numpy


I have dataframe containing distances as float64 values.

import numpy as np
import pandas as pd
binsize = 0.05
df = pd.DataFrame()
df['distance'] = [0.01555, 0.6, 0.99, 1.24] 

This returns:

    distance
0   0.01555
1   0.60000
2   0.99000
3   1.24000

I would like to round the min and max values rounded to the closest multiple of binsize.

This is how I am currently doing this:

np.round(np.round(df['distance'].min() / binsize) * binsize, 2)
np.round(np.round(df['distance'].max() / binsize) * binsize, 2)

Thus returning 0.0 and 1.25 for the above example.

Is there an easier way to achieve this?


Solution

  • You can use some old numeric tricks if you want, this gets to the same place:

    def quick_round(binsize, item):
        return (((item + binsize / 2) * (1 / binsize)) // 1) * binsize
    
    print(quick_round(binsize,df['distance'].min()), quick_round(binsize,df['distance'].max()))
    

    Yields:

    0.0 1.25