code: def from_multi_index_to_nested(
      multi_ind_dataframe, instance_index=None, cells_as_numpy=False
  ):
      """Convert a pandas DataFrame with multi-index to a nested DataFrame.
  
      Parameters
      ----------
      multi_ind_dataframe : pd.DataFrame
          Input multi-indexed pandas DataFrame
  
      instance_index_name : int or str, default=0 (first level = 0-th index)
          Index or name of multi-index level corresponding to the DataFrame's instances
  
      cells_as_numpy : bool, default = False
          If True, then nested cells contain NumPy array
          If False, then nested cells contain pandas Series
  
      Returns
      -------
      x_nested : pd.DataFrame
          The nested version of the DataFrame
      """
      if instance_index is None:
          instance_index = 0
  
      # get number of distinct cases (note: a case may have 1 or many dimensions)
      instance_idxs = multi_ind_dataframe.index.get_level_values(
          instance_index
      ).unique()
  
      x_nested = pd.DataFrame()
  
      # Loop the dimensions (columns) of multi-index DataFrame
      for _label, _series in multi_ind_dataframe.items():  # noqa
          # for _label in multi_ind_dataframe.columns:
          #    _series = multi_ind_dataframe.loc[:, _label]
          # Slice along the instance dimension to return list of series for each case
          # Note: if you omit .rename_axis the returned DataFrame
          #       prints time axis dimension at the start of each cell,
          #       but this doesn't affect the values.
          if cells_as_numpy:
              dim_list = [
                  _series.xs(instance_idx, level=instance_index).values
                  for instance_idx in instance_idxs
              ]
          else:
              dim_list = [
                  _series.xs(
                      instance_idx, level=instance_index
                  ).rename_axis(None)
                  for instance_idx in instance_idxs
              ]
  
          x_nested[_label] = pd.Series(dim_list)
      x_nested = pd.DataFrame(x_nested).set_axis(instance_idxs)
  
      col_msg = "Multi-index and nested DataFrames should have same columns names"
      assert (
          x_nested.columns == multi_ind_dataframe.columns
      ).all(), col_msg
  
      return x_nested
  
imports: import numpy
  import pandas
  from sktime.datatypes._convert_utils._coerce import _coerce_df_dtypes
  from sktime.datatypes._convert_utils._convert import _extend_conversions
  from sktime.datatypes._panel._registry import MTYPE_LIST_PANEL
  from sktime.utils.pandas import df_map
  from sktime.utils.validation._dependencies import _check_soft_dependencies
label_desc: Use image node_id 31 for calling a global function with example usage: from_multi_index_to_nested(multi_ind_dataframe, instance_index, cells_as_numpy) and returns: x_nested