code: def _try_cast(arr, take_fast_path, dtype, copy, raise_cast_failure):
      # perf shortcut as this is the most common case
      if take_fast_path:
          if maybe_castable(arr) and not copy and dtype is None:
              return arr
  
      try:
          # GH#15832: Check if we are requesting a numeric dype and
          # that we can convert the data to the requested dtype.
          if is_integer_dtype(dtype):
              subarr = maybe_cast_to_integer_array(arr, dtype)
  
          subarr = maybe_cast_to_datetime(arr, dtype)
          # Take care in creating object arrays (but iterators are not
          # supported):
          if is_object_dtype(dtype) and (
              is_list_like(subarr)
              and not (
                  is_iterator(subarr) or isinstance(subarr, np.ndarray)
              )
          ):
              subarr = construct_1d_object_array_from_listlike(subarr)
          elif not is_extension_type(subarr):
              subarr = construct_1d_ndarray_preserving_na(
                  subarr, dtype, copy=copy
              )
      except (ValueError, TypeError):
          if is_categorical_dtype(dtype):
              # We *do* allow casting to categorical, since we know
              # that Categorical is the only array type for 'category'.
              subarr = Categorical(
                  arr, dtype.categories, ordered=dtype.ordered
              )
          elif is_extension_array_dtype(dtype):
              # create an extension array from its dtype
              array_type = dtype.construct_array_type()._from_sequence
              subarr = array_type(arr, dtype=dtype, copy=copy)
          elif dtype is not None and raise_cast_failure:
              raise
          else:
              subarr = np.array(arr, dtype=object, copy=copy)
      return subarr
  
imports: from collections import OrderedDict
  import numpy
  import numpy.ma
  from pandas._libs import lib
  from pandas._libs.tslibs import IncompatibleFrequency
  import pandas.compat
  from pandas.compat import get_range_parameters, lmap, lrange, raise_with_traceback, range
  from pandas.core.dtypes.cast import construct_1d_arraylike_from_scalar, construct_1d_ndarray_preserving_na, construct_1d_object_array_from_listlike, infer_dtype_from_scalar, maybe_cast_to_datetime, maybe_cast_to_integer_array, maybe_castable, maybe_convert_platform, maybe_infer_to_datetimelike, maybe_upcast
  from pandas.core.dtypes.common import is_categorical_dtype, is_datetime64tz_dtype, is_dtype_equal, is_extension_array_dtype, is_extension_type, is_float_dtype, is_integer_dtype, is_iterator, is_list_like, is_object_dtype, pandas_dtype
  from pandas.core.dtypes.generic import ABCDataFrame, ABCDatetimeIndex, ABCIndexClass, ABCPandasArray, ABCPeriodIndex, ABCSeries, ABCTimedeltaIndex
  from pandas.core.dtypes.missing import isna
  from pandas.core import algorithms, common
  from pandas.core.arrays import Categorical, ExtensionArray, period_array
  from pandas.core.index import Index, _get_objs_combined_axis, _union_indexes, ensure_index
  from pandas.core.indexes import base
  from pandas.core.internals import create_block_manager_from_arrays, create_block_manager_from_blocks
  from pandas.core.internals.arrays import extract_array
label_desc: Use image node_id 18 for calling a global function with example usage: _try_cast(arr, take_fast_path, dtype, copy, raise_cast_failure) and returns: subarr, arr