import re from typing import Tuple def get_granularity(freq_str: str) -> Tuple[int, str]: """ Splits a frequency string such as "7D" into the multiple 7 and the base granularity "D". Parameters ---------- freq_str Frequency string of the form [multiple][granularity] such as "12H", "5min", "1D" etc. """ freq_regex = r"\s*((\d+)?)\s*([^\d]\w*)" m = re.match(freq_regex, freq_str) assert m is not None, "Cannot parse frequency string: %s" % freq_str groups = m.groups() multiple = int(groups[1]) if groups[1] is not None else 1 granularity = groups[2] return multiple, granularity