code: def _try_cast(
      arr: list | np.ndarray,
      dtype: np.dtype,
      copy: bool,
  ) -> ArrayLike:
      """
      Convert input to numpy ndarray and optionally cast to a given dtype.
  
      Parameters
      ----------
      arr : ndarray or list
          Excludes: ExtensionArray, Series, Index.
      dtype : np.dtype
      copy : bool
          If False, don't copy the data if not needed.
  
      Returns
      -------
      np.ndarray or ExtensionArray
      """
      is_ndarray = isinstance(arr, np.ndarray)
  
      if is_object_dtype(dtype):
          if not is_ndarray:
              subarr = construct_1d_object_array_from_listlike(arr)
              return subarr
          return ensure_wrapped_if_datetimelike(arr).astype(
              dtype, copy=copy
          )
  
      elif dtype.kind == "U":
          # TODO: test cases with arr.dtype.kind in ["m", "M"]
          if is_ndarray:
              arr = cast(np.ndarray, arr)
              shape = arr.shape
              if arr.ndim > 1:
                  arr = arr.ravel()
          else:
              shape = (len(arr),)
          return lib.ensure_string_array(
              arr, convert_na_value=False, copy=copy
          ).reshape(shape)
  
      elif dtype.kind in ["m", "M"]:
          return maybe_cast_to_datetime(arr, dtype)
  
      # GH#15832: Check if we are requesting a numeric dtype and
      # that we can convert the data to the requested dtype.
      elif is_integer_dtype(dtype):
          # this will raise if we have e.g. floats
  
          subarr = maybe_cast_to_integer_array(arr, dtype)
      else:
          subarr = np.array(arr, dtype=dtype, copy=copy)
  
      return subarr
  
imports: from __future__ import annotations
  from typing import TYPE_CHECKING, Optional, Sequence, Union, cast, overload
  import numpy
  from numpy import ma
  from pandas._libs import lib
  from pandas._libs.tslibs.period import Period
  from pandas._typing import AnyArrayLike, ArrayLike, Dtype, DtypeObj, T
  from pandas.core.dtypes.base import ExtensionDtype, _registry
  from pandas.core.dtypes.cast import construct_1d_arraylike_from_scalar, construct_1d_object_array_from_listlike, maybe_cast_to_datetime, maybe_cast_to_integer_array, maybe_convert_platform, maybe_infer_to_datetimelike, maybe_promote
  from pandas.core.dtypes.common import is_datetime64_ns_dtype, is_dtype_equal, is_extension_array_dtype, is_integer_dtype, is_list_like, is_object_dtype, is_timedelta64_ns_dtype
  from pandas.core.dtypes.dtypes import PandasDtype
  from pandas.core.dtypes.generic import ABCDataFrame, ABCExtensionArray, ABCIndex, ABCPandasArray, ABCRangeIndex, ABCSeries
  from pandas.core.dtypes.missing import isna
  import pandas.core.common
label_desc: Use image node_id 13 for calling a global function with example usage: _try_cast(arr, dtype, copy) and returns: subarr, ensure_wrapped_if_datetimelike, subarr, lib, maybe_cast_to_datetime