def value_to_bin_center(val, **kwargs):
"""Convert value to bin center
Convert a numeric or timestamp column to a common bin center value.
:param bin_width: bin_width value needed to convert column to a common bin center value
:param bin_offset: bin_offset value needed to convert column to a common bin center value
"""
try:
# NOTE this notation also works for timestamps, and does not change the
# unit
bin_width = kwargs.get('bin_width', 1)
bin_offset = kwargs.get('bin_offset', 0)
bin_index = int(np.floor((val - bin_offset) / bin_width))
obj_type = type(bin_width)
return bin_offset + obj_type((bin_index + 0.5) * bin_width)
except BaseException:
pass
return val
评论列表
文章目录