Skip to main content
`Ctrl`+`K`

# pandas.tseries.frequencies.to_offset

pandas.tseries.frequencies.to_offset(freq, is_period=False)

  Return DateOffset object from string or datetime.timedelta object.

  This function converts a frequency string (e.g. `"5min"`, `"1D1h"`, `"2W"`) or a `datetime.timedelta` into a pandas `DateOffset` subclass. It can also pass through existing `BaseOffset` instances unchanged.

  Parameters:
    freq str, datetime.timedelta, BaseOffset or None

      The frequency represented.

    is_period bool, default False

      Convert string denoting period frequency to corresponding offsets frequency if is_period=True.

  Returns:
    BaseOffset subclass or None
  Raises:
    ValueError

      If freq is an invalid frequency

  See also

  `BaseOffset`

    Standard kind of date increment used for a date range.

  Examples

      >>> from pandas.tseries.frequencies import to_offset
      >>> to_offset("5min")
      <5 * Minutes>

      >>> to_offset("1D1h")
      <25 * Hours>

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

      >>> to_offset("2B")
      <2 * BusinessDays>

      >>> to_offset(pd.Timedelta(days=1))
      <24 * Hours>

      >>> to_offset(pd.offsets.Hour())
      <Hour>

  Passing the parameter `is_period` equal to True, you can use a string denoting period frequency:

      >>> freq = to_offset(freq="ME", is_period=False)
      >>> freq.rule_code
      'ME'

      >>> freq = to_offset(freq="M", is_period=True)
      >>> freq.rule_code
      'ME'