code: def _cast_ndarray_columns_to_tensor_extension(
      df: "pd.DataFrame",
  ) -> "pd.DataFrame":
      """
      Cast all NumPy ndarray columns in df to our tensor extension type, TensorArray.
      """
      pd = _lazy_import_pandas()
      try:
          SettingWithCopyWarning = pd.core.common.SettingWithCopyWarning
      except AttributeError:
          # SettingWithCopyWarning was moved to pd.errors in Pandas 1.5.0.
          SettingWithCopyWarning = pd.errors.SettingWithCopyWarning
  
      from ray.air.util.tensor_extensions.pandas import (
          TensorArray,
          column_needs_tensor_extension,
      )
  
      # Try to convert any ndarray columns to TensorArray columns.
      # TODO(Clark): Once Pandas supports registering extension types for type
      # inference on construction, implement as much for NumPy ndarrays and remove
      # this. See https://github.com/pandas-dev/pandas/issues/41848
      # TODO(Clark): Optimize this with propagated DataFrame metadata containing a list of
      # column names containing tensor columns, to make this an O(# of tensor columns)
      # check rather than the current O(# of columns) check.
      for col_name, col in df.items():
          if column_needs_tensor_extension(col):
              try:
                  # Suppress Pandas warnings:
                  # https://github.com/ray-project/ray/issues/29270
                  # We actually want in-place operations so we surpress this warning.
                  # https://stackoverflow.com/a/74193599
                  with warnings.catch_warnings():
                      warnings.simplefilter(
                          "ignore", category=FutureWarning
                      )
                      warnings.simplefilter(
                          "ignore", category=SettingWithCopyWarning
                      )
                      df.loc[:, col_name] = TensorArray(col)
              except Exception as e:
                  raise ValueError(
                      f"Tried to cast column {col_name} to the TensorArray tensor "
                      "extension type but the conversion failed. To disable "
                      "automatic casting to this tensor extension, set "
                      "ctx = DataContext.get_current(); "
                      "ctx.enable_tensor_extension_casting = False."
                  ) from e
      return df
  
imports: from enum import Enum
  from typing import Dict, Union, List, TYPE_CHECKING
  import warnings
  import numpy
  from ray.air.data_batch_type import DataBatchType
  from ray.air.constants import TENSOR_COLUMN_NAME
  from ray.util.annotations import Deprecated, DeveloperAPI
label_desc: Use image node_id 9 for calling a global function with example usage: _cast_ndarray_columns_to_tensor_extension(df) and returns: df