code: def _transform_index(index, func, level=None):
      
      if isinstance(index, MultiIndex):
          if level is not None:
              items = [tuple(func(y) if i == level else y
                             for i, y in enumerate(x)) for x in index]
          else:
              items = [tuple(func(y) for y in x) for x in index]
          return MultiIndex.from_tuples(items, names=index.names)
      else:
          items = [func(x) for x in index]
          return Index(items, name=index.name, tupleize_cols=False)
docs: Apply function to all values found in index.
  
      This includes transforming multiindex entries separately.
      Only apply function to one level of the MultiIndex if level is specified.