code: def test_dataframe_apply():
      cols = [chr(ord("A") + i) for i in range(10)]
      df_raw = pd.DataFrame(
          dict((c, [i**2 for i in range(20)]) for c in cols)
      )
  
      old_chunk_store_limit = options.chunk_store_limit
      try:
          options.chunk_store_limit = 20
  
          df = from_pandas_df(df_raw, chunk_size=5)
  
          def df_func_with_err(v):
              assert len(v) > 2
              return v.sort_values()
  
          def df_series_func_with_err(v):
              assert len(v) > 2
              return 0
  
          with pytest.raises(TypeError):
              df.apply(df_func_with_err)
  
          r = df.apply(
              df_func_with_err,
              output_type="dataframe",
              dtypes=df_raw.dtypes,
          )
          assert r.shape == (np.nan, df.shape[-1])
          assert r.op._op_type_ == opcodes.APPLY
          assert r.op.output_types[0] == OutputType.dataframe
          assert r.op.elementwise is False
  
          r = df.apply(
              df_series_func_with_err,
              output_type="series",
              dtype=object,
              name="output",
          )
          assert r.dtype == np.dtype("O")
          assert r.shape == (df.shape[-1],)
          assert r.op._op_type_ == opcodes.APPLY
          assert r.op.output_types[0] == OutputType.series
          assert r.op.elementwise is False
  
          r = df.apply("ffill")
          assert r.op._op_type_ == opcodes.FILL_NA
  
          r = tile(df.apply(np.sqrt))
          assert all(v == np.dtype("float64") for v in r.dtypes) is True
          assert r.shape == df.shape
          assert r.op._op_type_ == opcodes.APPLY
          assert r.op.output_types[0] == OutputType.dataframe
          assert r.op.elementwise is True
  
          r = tile(df.apply(lambda x: pd.Series([1, 2])))
          assert all(v == np.dtype("int64") for v in r.dtypes) is True
          assert r.shape == (np.nan, df.shape[1])
          assert r.op.output_types[0] == OutputType.dataframe
          assert r.chunks[0].shape == (np.nan, 1)
          assert r.chunks[0].inputs[0].shape[0] == df_raw.shape[0]
          assert (
              r.chunks[0].inputs[0].op._op_type_ == opcodes.CONCATENATE
          )
          assert r.op.elementwise is False
  
          r = tile(df.apply(np.sum, axis="index"))
          assert np.dtype("int64") == r.dtype
          assert r.shape == (df.shape[1],)
          assert r.op.output_types[0] == OutputType.series
          assert r.chunks[0].shape == (20 // df.shape[0],)
          assert r.chunks[0].inputs[0].shape[0] == df_raw.shape[0]
          assert (
              r.chunks[0].inputs[0].op._op_type_ == opcodes.CONCATENATE
          )
          assert r.op.elementwise is False
  
          r = tile(df.apply(np.sum, axis="columns"))
          assert np.dtype("int64") == r.dtype
          assert r.shape == (df.shape[0],)
          assert r.op.output_types[0] == OutputType.series
          assert r.chunks[0].shape == (20 // df.shape[1],)
          assert r.chunks[0].inputs[0].shape[1] == df_raw.shape[1]
          assert (
              r.chunks[0].inputs[0].op._op_type_ == opcodes.CONCATENATE
          )
          assert r.op.elementwise is False
  
          r = tile(
              df.apply(
                  lambda x: pd.Series([1, 2], index=["foo", "bar"]),
                  axis=1,
              )
          )
          assert all(v == np.dtype("int64") for v in r.dtypes) is True
          assert r.shape == (df.shape[0], np.nan)
          assert r.op.output_types[0] == OutputType.dataframe
          assert r.chunks[0].shape == (20 // df.shape[1], np.nan)
          assert r.chunks[0].inputs[0].shape[1] == df_raw.shape[1]
          assert (
              r.chunks[0].inputs[0].op._op_type_ == opcodes.CONCATENATE
          )
          assert r.op.elementwise is False
  
          r = tile(
              df.apply(lambda x: [1, 2], axis=1, result_type="expand")
          )
          assert all(v == np.dtype("int64") for v in r.dtypes) is True
          assert r.shape == (df.shape[0], np.nan)
          assert r.op.output_types[0] == OutputType.dataframe
          assert r.chunks[0].shape == (20 // df.shape[1], np.nan)
          assert r.chunks[0].inputs[0].shape[1] == df_raw.shape[1]
          assert (
              r.chunks[0].inputs[0].op._op_type_ == opcodes.CONCATENATE
          )
          assert r.op.elementwise is False
  
          r = tile(
              df.apply(
                  lambda x: list(range(10)),
                  axis=1,
                  result_type="reduce",
              )
          )
          assert np.dtype("object") == r.dtype
          assert r.shape == (df.shape[0],)
          assert r.op.output_types[0] == OutputType.series
          assert r.chunks[0].shape == (20 // df.shape[1],)
          assert r.chunks[0].inputs[0].shape[1] == df_raw.shape[1]
          assert (
              r.chunks[0].inputs[0].op._op_type_ == opcodes.CONCATENATE
          )
          assert r.op.elementwise is False
  
          r = tile(
              df.apply(
                  lambda x: list(range(10)),
                  axis=1,
                  result_type="broadcast",
              )
          )
          assert all(v == np.dtype("int64") for v in r.dtypes) is True
          assert r.shape == (df.shape[0], np.nan)
          assert r.op.output_types[0] == OutputType.dataframe
          assert r.chunks[0].shape == (20 // df.shape[1], np.nan)
          assert r.chunks[0].inputs[0].shape[1] == df_raw.shape[1]
          assert (
              r.chunks[0].inputs[0].op._op_type_ == opcodes.CONCATENATE
          )
          assert r.op.elementwise is False
      finally:
          options.chunk_store_limit = old_chunk_store_limit
  
      raw = pd.DataFrame(
          {"a": [np.array([1, 2, 3]), np.array([4, 5, 6])]}
      )
      df = from_pandas_df(raw)
      df2 = df.apply(
          lambda x: x["a"].astype(pd.Series),
          axis=1,
          output_type="dataframe",
          dtypes=pd.Series([np.dtype(float)] * 3),
      )
      assert df2.ndim == 2
  
imports: from typing import List, Union
  import numpy
  import pandas
  import pytest
  from ....None import opcodes
  from ....config import options, option_context
  from ....core import OutputType, tile, Tileable
  from ....core.graph import TileableGraph, TileableGraphBuilder, TileContext, ChunkGraphBuilder
  from ....core.operand import OperandStage
  from ....tensor.core import TENSOR_TYPE
  from ...None import eval, cut, get_dummies, to_numeric
  from ...core import DATAFRAME_TYPE, SERIES_TYPE, SERIES_CHUNK_TYPE, INDEX_TYPE, CATEGORICAL_TYPE, CATEGORICAL_CHUNK_TYPE, DataFrameData, SeriesData
  from ...datasource.dataframe import from_pandas
  from ...datasource.series import from_pandas
  from ...datasource.index import from_pandas
  from ..None import to_gpu, to_cpu, astype
label_desc: Use image node_id 5 for calling a global function with example usage: test_dataframe_apply() and returns: v, int