def to_offset(freq):
if freq is None:<EOL><INDENT>return None<EOL><DEDENT>if isinstance(freq, DateOffset):<EOL><INDENT>return freq<EOL><DEDENT>if isinstance(freq, tuple):<EOL><INDENT>name = freq[<NUM_LIT:0>]<EOL>stride = freq[<NUM_LIT:1>]<EOL>if isinstance(stride, str):<EOL><INDENT>name, stride = stride, name<EOL><DEDENT>name, _ = libfreqs._base_and_stride(name)<EOL>delta = get_offset(name) * stride<EOL><DEDENT>elif isinstance(freq, timedelta):<EOL><INDENT>delta = None<EOL>freq = Timedelta(freq)<EOL>try:<EOL><INDENT>for name in freq.components._fields:<EOL><INDENT>offset = _name_to_offset_map[name]<EOL>stride = getattr(freq.components, name)<EOL>if stride != <NUM_LIT:0>:<EOL><INDENT>offset = stride * offset<EOL>if delta is None:<EOL><INDENT>delta = offset<EOL><DEDENT>else:<EOL><INDENT>delta = delta + offset<EOL><DEDENT><DEDENT><DEDENT><DEDENT>except Exception:<EOL><INDENT>raise ValueError(libfreqs.INVALID_FREQ_ERR_MSG.format(freq))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>delta = None<EOL>stride_sign = None<EOL>try:<EOL><INDENT>splitted = re.split(libfreqs.opattern, freq)<EOL>if splitted[-<NUM_LIT:1>] != '<STR_LIT>' and not splitted[-<NUM_LIT:1>].isspace():<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>for sep, stride, name in zip(splitted[<NUM_LIT:0>::<NUM_LIT:4>], splitted[<NUM_LIT:1>::<NUM_LIT:4>],<EOL>splitted[<NUM_LIT:2>::<NUM_LIT:4>]):<EOL><INDENT>if sep != '<STR_LIT>' and not sep.isspace():<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>prefix = libfreqs._lite_rule_alias.get(name) or name<EOL>if stride_sign is None:<EOL><INDENT>stride_sign = -<NUM_LIT:1> if stride.startswith('<STR_LIT:->') else <NUM_LIT:1><EOL><DEDENT>if not stride:<EOL><INDENT>stride = <NUM_LIT:1><EOL><DEDENT>if prefix in Resolution._reso_str_bump_map.keys():<EOL><INDENT>stride, name = Resolution.get_stride_from_decimal(<EOL>float(stride), prefix<EOL>)<EOL><DEDENT>stride = int(stride)<EOL>offset = get_offset(name)<EOL>offset = offset * int(np.fabs(stride) * stride_sign)<EOL>if delta is None:<EOL><INDENT>delta = offset<EOL><DEDENT>else:<EOL><INDENT>delta = delta + offset<EOL><DEDENT><DEDENT><DEDENT>except Exception:<EOL><INDENT>raise ValueError(libfreqs.INVALID_FREQ_ERR_MSG.format(freq))<EOL><DEDENT><DEDENT>if delta is None:<EOL><INDENT>raise ValueError(libfreqs.INVALID_FREQ_ERR_MSG.format(freq))<EOL><DEDENT>return delta<EOL>
Return DateOffset object from string or tuple representation
or datetime.timedelta object

Parameters
----------
freq : str, tuple, datetime.timedelta, DateOffset or None

Returns
-------
DateOffset
    None if freq is None.

Raises
------
ValueError
    If freq is an invalid frequency

See Also
--------
DateOffset

Examples
--------
>>> to_offset('5min')
<5 * Minutes>

>>> to_offset('1D1H')
<25 * Hours>

>>> to_offset(('W', 2))
<2 * Weeks: weekday=6>

>>> to_offset((2, 'B'))
<2 * BusinessDays>

>>> to_offset(datetime.timedelta(days=1))
<Day>

>>> to_offset(Hour())
<Hour>