code: def maybe_cast_to_datetime(value, dtype, errors="raise"):
      """try to cast the array/value to a datetimelike dtype, converting float
      nan to iNaT
      """
      from pandas.core.tools.timedeltas import to_timedelta
      from pandas.core.tools.datetimes import to_datetime
  
      if dtype is not None:
          if isinstance(dtype, string_types):
              dtype = np.dtype(dtype)
  
          is_datetime64 = is_datetime64_dtype(dtype)
          is_datetime64tz = is_datetime64tz_dtype(dtype)
          is_timedelta64 = is_timedelta64_dtype(dtype)
  
          if is_datetime64 or is_datetime64tz or is_timedelta64:
              # Force the dtype if needed.
              msg = (
                  "The '{dtype}' dtype has no unit. "
                  "Please pass in '{dtype}[ns]' instead."
              )
  
              if is_datetime64 and not is_dtype_equal(dtype, _NS_DTYPE):
                  if dtype.name in ("datetime64", "datetime64[ns]"):
                      if dtype.name == "datetime64":
                          raise ValueError(msg.format(dtype=dtype.name))
                      dtype = _NS_DTYPE
                  else:
                      raise TypeError(
                          "cannot convert datetimelike to "
                          "dtype [{dtype}]".format(dtype=dtype)
                      )
              elif is_datetime64tz:
                  # our NaT doesn't support tz's
                  # this will coerce to DatetimeIndex with
                  # a matching dtype below
                  if is_scalar(value) and isna(value):
                      value = [value]
  
              elif is_timedelta64 and not is_dtype_equal(
                  dtype, _TD_DTYPE
              ):
                  if dtype.name in ("timedelta64", "timedelta64[ns]"):
                      if dtype.name == "timedelta64":
                          raise ValueError(msg.format(dtype=dtype.name))
                      dtype = _TD_DTYPE
                  else:
                      raise TypeError(
                          "cannot convert timedeltalike to "
                          "dtype [{dtype}]".format(dtype=dtype)
                      )
  
              if is_scalar(value):
                  if value == iNaT or isna(value):
                      value = iNaT
              else:
                  value = np.array(value, copy=False)
  
                  # have a scalar array-like (e.g. NaT)
                  if value.ndim == 0:
                      value = iNaT
  
                  # we have an array of datetime or timedeltas & nulls
                  elif np.prod(value.shape) or not is_dtype_equal(
                      value.dtype, dtype
                  ):
                      try:
                          if is_datetime64:
                              value = to_datetime(
                                  value, errors=errors
                              )._values
                          elif is_datetime64tz:
                              # The string check can be removed once issue #13712
                              # is solved. String data that is passed with a
                              # datetime64tz is assumed to be naive which should
                              # be localized to the timezone.
                              is_dt_string = is_string_dtype(value)
                              value = to_datetime(
                                  value, errors=errors
                              ).array
                              if is_dt_string:
                                  # Strings here are naive, so directly localize
                                  value = value.tz_localize(dtype.tz)
                              else:
                                  # Numeric values are UTC at this point,
                                  # so localize and convert
                                  value = value.tz_localize(
                                      "UTC"
                                  ).tz_convert(dtype.tz)
                          elif is_timedelta64:
                              value = to_timedelta(
                                  value, errors=errors
                              )._values
                      except (AttributeError, ValueError, TypeError):
                          pass
  
          # coerce datetimelike to object
          elif is_datetime64_dtype(value) and not is_datetime64_dtype(
              dtype
          ):
              if is_object_dtype(dtype):
                  if value.dtype != _NS_DTYPE:
                      value = value.astype(_NS_DTYPE)
                  ints = np.asarray(value).view("i8")
                  return tslib.ints_to_pydatetime(ints)
  
              # we have a non-castable dtype that was passed
              raise TypeError(
                  "Cannot cast datetime64 to {dtype}".format(
                      dtype=dtype
                  )
              )
  
      else:
          is_array = isinstance(value, np.ndarray)
  
          # catch a datetime/timedelta that is not of ns variety
          # and no coercion specified
          if is_array and value.dtype.kind in ["M", "m"]:
              dtype = value.dtype
  
              if dtype.kind == "M" and dtype != _NS_DTYPE:
                  value = value.astype(_NS_DTYPE)
  
              elif dtype.kind == "m" and dtype != _TD_DTYPE:
                  value = to_timedelta(value)
  
          # only do this if we have an array and the dtype of the array is not
          # setup already we are not an integer/object, so don't bother with this
          # conversion
          elif not (
              is_array
              and not (
                  issubclass(value.dtype.type, np.integer)
                  or value.dtype == np.object_
              )
          ):
              value = maybe_infer_to_datetimelike(value)
  
      return value
  
imports: from datetime import datetime, timedelta
  import numpy
  from pandas._libs import lib, tslib, tslibs
  from pandas._libs.tslibs import NaT, OutOfBoundsDatetime, Period, iNaT
  from pandas.compat import PY3, string_types, text_type, to_str
  from .common import _INT64_DTYPE, _NS_DTYPE, _POSSIBLY_CAST_DTYPES, _TD_DTYPE, ensure_int8, ensure_int16, ensure_int32, ensure_int64, ensure_object, is_bool, is_bool_dtype, is_categorical_dtype, is_complex, is_complex_dtype, is_datetime64_dtype, is_datetime64_ns_dtype, is_datetime64tz_dtype, is_datetime_or_timedelta_dtype, is_datetimelike, is_dtype_equal, is_extension_array_dtype, is_extension_type, is_float, is_float_dtype, is_integer, is_integer_dtype, is_object_dtype, is_scalar, is_string_dtype, is_timedelta64_dtype, is_timedelta64_ns_dtype, is_unsigned_integer_dtype, pandas_dtype
  from .dtypes import DatetimeTZDtype, ExtensionDtype, PandasExtensionDtype, PeriodDtype
  from .generic import ABCDatetimeArray, ABCDatetimeIndex, ABCPeriodArray, ABCPeriodIndex, ABCSeries
  from .inference import is_list_like
  from .missing import isna, notna
label_desc: Use image node_id 20 for calling a global function with example usage: maybe_cast_to_datetime(value, dtype, errors) and returns: value, tslib