code: def _coerce_to_dtype(value, dtype):
      if dtype == categorical_dtype:
          # This check is necessary because we use object dtype for
          # categoricals, and numpy will allow us to promote numerical
          # values to object even though we don't support them.
          _assert_valid_categorical_missing_value(value)
          return value
      else:
          # For any other type, cast using the same rules as numpy's astype
          # function with casting='same_kind'.
          #
          # 'same_kind' allows casting between things like float32 and float64,
          # but not between str and int. Note that the name is somewhat
          # misleading, since it does allow conversion between different dtype
          # kinds in some cases. In particular, conversion from int to float is
          # allowed.
          return array([value]).astype(
              dtype=dtype, casting="same_kind"
          )[0]
  
imports: from abc import ABCMeta, abstractproperty, abstractmethod
  from bisect import insort
  from collections import Mapping
  from weakref import WeakValueDictionary
  from numpy import array, dtype, ndarray
  from six import with_metaclass
  from zipline.assets import Asset
  from zipline.errors import DTypeNotSpecified, InvalidOutputName, NonSliceableTerm, NonWindowSafeInput, NotDType, NonPipelineInputs, TermInputsNotSpecified, TermOutputsEmpty, UnsupportedDType, WindowLengthNotSpecified
  from zipline.lib.adjusted_array import can_represent_dtype
  from zipline.lib.labelarray import LabelArray
  from zipline.utils.input_validation import expect_types
  from zipline.utils.memoize import classlazyval, lazyval
  from zipline.utils.numpy_utils import bool_dtype, categorical_dtype, datetime64ns_dtype, default_missing_value_for_dtype, float64_dtype
  from zipline.utils.sharedoc import templated_docstring, PIPELINE_ALIAS_NAME_DOC, PIPELINE_DOWNSAMPLING_FREQUENCY_DOC
  from .domain import Domain, GENERIC, infer_domain
  from .downsample_helpers import expect_downsample_frequency
  from .sentinels import NotSpecified
label_desc: Use image node_id 3 for calling a global function with example usage: _coerce_to_dtype(value, dtype) and returns: value, array