docstr: Helper providing null handling around applying an aggregation to a column.
docstr_tok: ["Helper","providing","null","handling","around","applying","an","aggregation","to","a","column","."]
code: def _apply_agg(
      self, agg_fn: Callable[["pandas.Series", bool], U], on: str
  ) -> Optional[U]:
      """Helper providing null handling around applying an aggregation to a column."""
      pd = lazy_import_pandas()
      if on is not None and not isinstance(on, str):
          raise ValueError(
              "on must be a string or None when aggregating on Pandas blocks, but "
              f"got: {type(on)}."
          )
  
      if self.num_rows() == 0:
          return None
  
      col = self._table[on]
      try:
          val = agg_fn(col)
      except TypeError as e:
          # Converting an all-null column in an Arrow Table to a Pandas DataFrame
          # column will result in an all-None column of object type, which will raise
          # a type error when attempting to do most binary operations. We explicitly
          # check for this type failure here so we can properly propagate a null.
          if (
              np.issubdtype(col.dtype, np.object_)
              and col.isnull().all()
          ):
              return None
          raise e from None
      if pd.isnull(val):
          return None
      return val
  
imports: import collections
  import heapq
  from typing import TYPE_CHECKING, Any, Callable, Dict, Iterator, List, Optional, Tuple, TypeVar, Union
  import numpy
  from ray.air.constants import TENSOR_COLUMN_NAME
  from ray.data._internal.table_block import TableBlockAccessor, TableBlockBuilder
  from ray.data._internal.util import find_partitions
  from ray.data.block import Block, BlockAccessor, BlockExecStats, BlockMetadata, KeyType, U
  from ray.data.context import DataContext
  from ray.data.row import TableRow
label_desc: Use image node_id 19 for calling the PandasBlockAccessor obj's underlying member method code with example usage: obj._apply_agg(agg_fn, on) and returns: val, None, None, None