def get_bin_center(self, bin_label):
"""Return bin center for a given bin index
:param bin_label: bin label for which to find the bin center
:returns: bin center, can be float, int, timestamp
"""
if not self.bin_specs:
return None
bin_idx = np.int64(bin_label)
if 'bin_edges' in self.bin_specs:
bin_edges = self.bin_specs['bin_edges']
if bin_idx < 0 or bin_idx >= len(bin_edges):
raise RuntimeError('bin_label "%s" does not fit in bin edges' % bin_label)
# NOTE: computation below also works with timestamps! Order is
# important.
bin_width = bin_edges[bin_idx + 1] - bin_edges[bin_idx]
bin_width_half = bin_width / 2.
bin_center = bin_edges[bin_idx] + bin_width_half
else:
width = self.bin_specs['bin_width']
offset = self.bin_specs.get('bin_offset', 0.)
# NOTE: this notation also works with timestamps!
bin_center = offset + (bin_idx + 0.5) * width
return bin_center
评论列表
文章目录