def maybe_cast_to_datetime(value, dtype, errors='<STR_LIT>'):
from pandas.core.tools.timedeltas import to_timedelta<EOL>from pandas.core.tools.datetimes import to_datetime<EOL>if dtype is not None:<EOL><INDENT>if isinstance(dtype, str):<EOL><INDENT>dtype = np.dtype(dtype)<EOL><DEDENT>is_datetime64 = is_datetime64_dtype(dtype)<EOL>is_datetime64tz = is_datetime64tz_dtype(dtype)<EOL>is_timedelta64 = is_timedelta64_dtype(dtype)<EOL>if is_datetime64 or is_datetime64tz or is_timedelta64:<EOL><INDENT>msg = ("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>if is_datetime64 and not is_dtype_equal(dtype, _NS_DTYPE):<EOL><INDENT>if dtype.name in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>if dtype.name == '<STR_LIT>':<EOL><INDENT>raise ValueError(msg.format(dtype=dtype.name))<EOL><DEDENT>dtype = _NS_DTYPE<EOL><DEDENT>else:<EOL><INDENT>raise TypeError("<STR_LIT>"<EOL>"<STR_LIT>".format(dtype=dtype))<EOL><DEDENT><DEDENT>elif is_datetime64tz:<EOL><INDENT>if is_scalar(value) and isna(value):<EOL><INDENT>value = [value]<EOL><DEDENT><DEDENT>elif is_timedelta64 and not is_dtype_equal(dtype, _TD_DTYPE):<EOL><INDENT>if dtype.name in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>if dtype.name == '<STR_LIT>':<EOL><INDENT>raise ValueError(msg.format(dtype=dtype.name))<EOL><DEDENT>dtype = _TD_DTYPE<EOL><DEDENT>else:<EOL><INDENT>raise TypeError("<STR_LIT>"<EOL>"<STR_LIT>".format(dtype=dtype))<EOL><DEDENT><DEDENT>if is_scalar(value):<EOL><INDENT>if value == iNaT or isna(value):<EOL><INDENT>value = iNaT<EOL><DEDENT><DEDENT>else:<EOL><INDENT>value = np.array(value, copy=False)<EOL>if value.ndim == <NUM_LIT:0>:<EOL><INDENT>value = iNaT<EOL><DEDENT>elif np.prod(value.shape) or not is_dtype_equal(value.dtype,<EOL>dtype):<EOL><INDENT>try:<EOL><INDENT>if is_datetime64:<EOL><INDENT>value = to_datetime(value, errors=errors)<EOL>if value.tz is not None:<EOL><INDENT>value = value.tz_localize(None)<EOL><DEDENT>value = value._values<EOL><DEDENT>elif is_datetime64tz:<EOL><INDENT>is_dt_string = is_string_dtype(value)<EOL>value = to_datetime(value, errors=errors).array<EOL>if is_dt_string:<EOL><INDENT>value = value.tz_localize(dtype.tz)<EOL><DEDENT>else:<EOL><INDENT>value = (value.tz_localize('<STR_LIT>')<EOL>.tz_convert(dtype.tz))<EOL><DEDENT><DEDENT>elif is_timedelta64:<EOL><INDENT>value = to_timedelta(value, errors=errors)._values<EOL><DEDENT><DEDENT>except (AttributeError, ValueError, TypeError):<EOL><INDENT>pass<EOL><DEDENT><DEDENT><DEDENT><DEDENT>elif is_datetime64_dtype(value) and not is_datetime64_dtype(dtype):<EOL><INDENT>if is_object_dtype(dtype):<EOL><INDENT>if value.dtype != _NS_DTYPE:<EOL><INDENT>value = value.astype(_NS_DTYPE)<EOL><DEDENT>ints = np.asarray(value).view('<STR_LIT>')<EOL>return tslib.ints_to_pydatetime(ints)<EOL><DEDENT>raise TypeError('<STR_LIT>'<EOL>.format(dtype=dtype))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>is_array = isinstance(value, np.ndarray)<EOL>if is_array and value.dtype.kind in ['<STR_LIT:M>', '<STR_LIT:m>']:<EOL><INDENT>dtype = value.dtype<EOL>if dtype.kind == '<STR_LIT:M>' and dtype != _NS_DTYPE:<EOL><INDENT>value = value.astype(_NS_DTYPE)<EOL><DEDENT>elif dtype.kind == '<STR_LIT:m>' and dtype != _TD_DTYPE:<EOL><INDENT>value = to_timedelta(value)<EOL><DEDENT><DEDENT>elif not (is_array and not (issubclass(value.dtype.type, np.integer) or<EOL>value.dtype == np.object_)):<EOL><INDENT>value = maybe_infer_to_datetimelike(value)<EOL><DEDENT><DEDENT>return value<EOL>
try to cast the array/value to a datetimelike dtype, converting float
    nan to iNaT