code: def infer_freq(index: pd.Index) -> str:
      if isinstance(index, pd.PeriodIndex):
          return index.freqstr
  
      freq = pd.infer_freq(index)
      # pandas likes to infer the `start of x` frequency, however when doing
      # df.to_period("<x>S"), it fails, so we avoid using it. It's enough to
      # remove the trailing S, e.g `MS` -> `M
      if len(freq) > 1 and freq.endswith("S"):
          return freq[:-1]
  
      return freq
  
imports: from __future__ import annotations
  import logging
  from dataclasses import dataclass, field, InitVar
  from typing import Any, Iterable, Optional, Type, Union, cast
  import numpy
  import pandas
  from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin
  from toolz import first
  from gluonts import maybe
  from gluonts.dataset.common import DataEntry
  from gluonts.itertools import Map, StarMap, SizedIterable
label_desc: Use image node_id 2 for calling a global function with example usage: infer_freq(index) and returns: freq, index, freq