code: def from_3d_numpy_to_nested(
      X, column_names=None, cells_as_numpy=False
  ):
      """Convert numpy 3D Panel to nested pandas Panel.
  
      Convert NumPy ndarray with shape (n_instances, n_columns, n_timepoints)
      into nested pandas DataFrame (with time series as pandas Series in cells)
  
      Parameters
      ----------
      X : np.ndarray
          3-dimensional Numpy array to convert to nested pandas DataFrame format
  
      column_names: list-like, default = None
          Optional list of names to use for naming nested DataFrame's columns
  
      cells_as_numpy : bool, default = False
          If True, then nested cells contain NumPy array
          If False, then nested cells contain pandas Series
  
  
      Returns
      -------
      df : pd.DataFrame
      """
      n_instances, n_columns, n_timepoints = X.shape
      array_type = X.dtype
  
      container = np.array if cells_as_numpy else pd.Series
  
      if column_names is None:
          column_names = _make_column_names(n_columns)
  
      else:
          if len(column_names) != n_columns:
              msg = " ".join(
                  [
                      f"Input 3d Numpy array as {n_columns} columns,",
                      f"but only {len(column_names)} names supplied",
                  ]
              )
              raise ValueError(msg)
  
      column_list = []
      for j, column in enumerate(column_names):
          nested_column = (
              pd.DataFrame(X[:, j, :])
              .apply(lambda x: [container(x, dtype=array_type)], axis=1)
              .str[0]
              .rename(column)
          )
          column_list.append(nested_column)
      df = pd.concat(column_list, axis=1)
      return df
  
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 38 for calling a global function with example usage: from_3d_numpy_to_nested(X, column_names, cells_as_numpy) and returns: df