code: def datetime_groupby_apply(
      df,
      apply_func: Union[Callable, Text],
      axis=0,
      level="datetime",
      resample_rule="M",
      n_jobs=-1,
  ):
      """datetime_groupby_apply
      This function will apply the `apply_func` on the datetime level index.
  
      Parameters
      ----------
      df :
          DataFrame for processing
      apply_func : Union[Callable, Text]
          apply_func for processing the data
          if a string is given, then it is treated as naive pandas function
      axis :
          which axis is the datetime level located
      level :
          which level is the datetime level
      resample_rule :
          How to resample the data to calculating parallel
      n_jobs :
          n_jobs for joblib
      Returns:
          pd.DataFrame
      """
  
      def _naive_group_apply(df):
          if isinstance(apply_func, str):
              return getattr(
                  df.groupby(axis=axis, level=level), apply_func
              )()
          return df.groupby(axis=axis, level=level).apply(apply_func)
  
      if n_jobs != 1:
          dfs = ParallelExt(n_jobs=n_jobs)(
              delayed(_naive_group_apply)(sub_df)
              for idx, sub_df in df.resample(
                  resample_rule, axis=axis, level=level
              )
          )
          return pd.concat(dfs, axis=axis).sort_index()
      else:
          return _naive_group_apply(df)
  
imports: from functools import partial
  from threading import Thread
  from typing import Callable, Text, Union
  from joblib import Parallel, delayed
  from joblib._parallel_backends import MultiprocessingBackend
  import pandas
  from queue import Queue
  import concurrent
  from qlib.config import C, QlibConfig
label_desc: Use image node_id 1 for calling a global function with example usage: datetime_groupby_apply(df, apply_func, axis, level, resample_rule, n_jobs) and returns: df, pd, _naive_group_apply, getattr