code: def infer_dtype_from_scalar(
      val, pandas_dtype: bool = False
  ) -> tuple[DtypeObj, Any]:
      """
      Interpret the dtype from a scalar.
  
      Parameters
      ----------
      pandas_dtype : bool, default False
          whether to infer dtype including pandas extension types.
          If False, scalar belongs to pandas extension types is inferred as
          object
      """
      dtype: DtypeObj = _dtype_obj
  
      # a 1-element ndarray
      if isinstance(val, np.ndarray):
          if val.ndim != 0:
              msg = "invalid ndarray passed to infer_dtype_from_scalar"
              raise ValueError(msg)
  
          dtype = val.dtype
          val = lib.item_from_zerodim(val)
  
      elif isinstance(val, str):
          # If we create an empty array using a string to infer
          # the dtype, NumPy will only allocate one character per entry
          # so this is kind of bad. Alternately we could use np.repeat
          # instead of np.empty (but then you still don't want things
          # coming out as np.str_!
  
          dtype = _dtype_obj
  
      elif isinstance(val, (np.datetime64, dt.datetime)):
          try:
              val = Timestamp(val)
              if val is not NaT:
                  val = val.as_unit("ns")
          except OutOfBoundsDatetime:
              return _dtype_obj, val
  
          if val is NaT or val.tz is None:
              val = val.to_datetime64()
              dtype = val.dtype
              # TODO: test with datetime(2920, 10, 1) based on test_replace_dtypes
          else:
              if pandas_dtype:
                  dtype = DatetimeTZDtype(unit="ns", tz=val.tz)
              else:
                  # return datetimetz as object
                  return _dtype_obj, val
  
      elif isinstance(val, (np.timedelta64, dt.timedelta)):
          try:
              val = Timedelta(val)
          except (OutOfBoundsTimedelta, OverflowError):
              dtype = _dtype_obj
          else:
              dtype = np.dtype("m8[ns]")
              val = np.timedelta64(val.value, "ns")
  
      elif is_bool(val):
          dtype = np.dtype(np.bool_)
  
      elif is_integer(val):
          if isinstance(val, np.integer):
              dtype = np.dtype(type(val))
          else:
              dtype = np.dtype(np.int64)
  
          try:
              np.array(val, dtype=dtype)
          except OverflowError:
              dtype = np.array(val).dtype
  
      elif is_float(val):
          if isinstance(val, np.floating):
              dtype = np.dtype(type(val))
          else:
              dtype = np.dtype(np.float64)
  
      elif is_complex(val):
          dtype = np.dtype(np.complex_)
  
      elif pandas_dtype:
          if lib.is_period(val):
              dtype = PeriodDtype(freq=val.freq)
          elif lib.is_interval(val):
              subtype = infer_dtype_from_scalar(
                  val.left, pandas_dtype=True
              )[0]
              dtype = IntervalDtype(subtype=subtype, closed=val.closed)
  
      return dtype, val
  
imports: from __future__ import annotations
  import datetime
  import functools
  from typing import TYPE_CHECKING, Any, Literal, Sized, TypeVar, cast, overload
  import warnings
  import numpy
  from pandas._libs import lib
  from pandas._libs.missing import NA, NAType, checknull
  from pandas._libs.tslibs import NaT, OutOfBoundsDatetime, OutOfBoundsTimedelta, Timedelta, Timestamp, get_unit_from_dtype, is_supported_unit
  from pandas._libs.tslibs.timedeltas import array_to_timedelta64
  from pandas._typing import ArrayLike, Dtype, DtypeObj, NumpyIndexT, Scalar, npt
  from pandas.errors import IntCastingNaNError, LossySetitemError
  from pandas.core.dtypes.common import ensure_int8, ensure_int16, ensure_int32, ensure_int64, ensure_object, ensure_str, is_bool, is_bool_dtype, is_complex, is_complex_dtype, is_datetime64_dtype, is_extension_array_dtype, is_float, is_float_dtype, is_integer, is_integer_dtype, is_numeric_dtype, is_object_dtype, is_scalar, is_signed_integer_dtype, is_string_dtype, is_timedelta64_dtype, is_unsigned_integer_dtype, pandas_dtype
  from pandas.core.dtypes.dtypes import BaseMaskedDtype, CategoricalDtype, DatetimeTZDtype, ExtensionDtype, IntervalDtype, PandasExtensionDtype, PeriodDtype
  from pandas.core.dtypes.generic import ABCExtensionArray, ABCIndex, ABCSeries
  from pandas.core.dtypes.inference import is_list_like
  from pandas.core.dtypes.missing import is_valid_na_for_dtype, isna, na_value_for_dtype, notna
  from pandas.io._util import _arrow_dtype_mapping
label_desc: Use image node_id 24 for calling a global function with example usage: infer_dtype_from_scalar(val, pandas_dtype) and returns: dtype,  val, _dtype_obj,  val, _dtype_obj,  val