code: def test_groupby_apply(setup):
      df1 = pd.DataFrame(
          {
              "a": [3, 4, 5, 3, 5, 4, 1, 2, 3],
              "b": [1, 3, 4, 5, 6, 5, 4, 4, 4],
              "c": list("aabaaddce"),
          }
      )
  
      def apply_df(df, ret_series=False):
          df = df.sort_index()
          df.a += df.b
          if len(df.index) > 0:
              if not ret_series:
                  df = df.iloc[:-1, :]
              else:
                  df = df.iloc[-1, :]
          return df
  
      def apply_series(s, truncate=True):
          s = s.sort_index()
          if truncate and len(s.index) > 0:
              s = s.iloc[:-1]
          return s
  
      mdf = md.DataFrame(df1, chunk_size=3)
  
      applied = mdf.groupby("b").apply(lambda df: None)
      pd.testing.assert_frame_equal(
          applied.execute().fetch(),
          df1.groupby("b").apply(lambda df: None),
      )
  
      applied = mdf.groupby("b").apply(apply_df)
      pd.testing.assert_frame_equal(
          applied.execute().fetch().sort_index(),
          df1.groupby("b").apply(apply_df).sort_index(),
      )
  
      applied = mdf.groupby("b").apply(apply_df, ret_series=True)
      pd.testing.assert_frame_equal(
          applied.execute().fetch().sort_index(),
          df1.groupby("b")
          .apply(apply_df, ret_series=True)
          .sort_index(),
      )
  
      applied = mdf.groupby("b").apply(
          lambda df: df.a, output_type="series"
      )
      pd.testing.assert_series_equal(
          applied.execute().fetch().sort_index(),
          df1.groupby("b").apply(lambda df: df.a).sort_index(),
      )
  
      applied = mdf.groupby("b").apply(lambda df: df.a.sum())
      pd.testing.assert_series_equal(
          applied.execute().fetch().sort_index(),
          df1.groupby("b").apply(lambda df: df.a.sum()).sort_index(),
      )
  
      series1 = pd.Series([3, 4, 5, 3, 5, 4, 1, 2, 3])
      ms1 = md.Series(series1, chunk_size=3)
  
      applied = ms1.groupby(lambda x: x % 3).apply(lambda df: None)
      pd.testing.assert_series_equal(
          applied.execute().fetch(),
          series1.groupby(lambda x: x % 3).apply(lambda df: None),
      )
  
      applied = ms1.groupby(lambda x: x % 3).apply(apply_series)
      pd.testing.assert_series_equal(
          applied.execute().fetch().sort_index(),
          series1.groupby(lambda x: x % 3)
          .apply(apply_series)
          .sort_index(),
      )
  
      sindex2 = pd.MultiIndex.from_arrays(
          [list(range(9)), list("ABCDEFGHI")]
      )
      series2 = pd.Series(list("CDECEDABC"), index=sindex2)
      ms2 = md.Series(series2, chunk_size=3)
  
      applied = ms2.groupby(lambda x: x[0] % 3).apply(apply_series)
      pd.testing.assert_series_equal(
          applied.execute().fetch().sort_index(),
          series2.groupby(lambda x: x[0] % 3)
          .apply(apply_series)
          .sort_index(),
      )
  
imports: from collections import OrderedDict
  import numpy
  import pandas
  import pytest
  from ....None import dataframe
  from ....config import option_context
  from ....core.operand import OperandStage
  from ....tests.core import assert_groupby_equal, require_cudf
  from ....utils import arrow_array_to_objects, pd_release_version
  from ...core import DATAFRAME_OR_SERIES_TYPE
  from ..aggregation import DataFrameGroupByAgg
label_desc: Use image node_id 10 for calling a global function with example usage: test_groupby_apply(setup) and returns: df, s