code: def sequence_to_pyseries(
      name: str,
      values: Sequence[Any],
      dtype: PolarsDataType | None = None,
      *,
      dtype_if_empty: PolarsDataType = Null,
      strict: bool = True,
      nan_to_null: bool = False,
  ) -> PySeries:
      """Construct a PySeries from a sequence."""
      python_dtype: type | None = None
  
      # empty sequence
      if not values and dtype is None:
          # if dtype for empty sequence could be guessed
          # (e.g comparisons between self and other), default to Null
          dtype = dtype_if_empty
  
      # lists defer to subsequent handling; identify nested type
      elif dtype == List:
          getattr(dtype, "inner", None)
          python_dtype = list
  
      # infer temporal type handling
      py_temporal_types = {date, datetime, timedelta, time}
      pl_temporal_types = {Date, Datetime, Duration, Time}
  
      value = _get_first_non_none(values)
      if value is not None:
          if (
              dataclasses.is_dataclass(value)
              or is_pydantic_model(value)
              or is_namedtuple(value.__class__)
          ):
              return pl.DataFrame(values).to_struct(name)._s
          elif isinstance(value, range):
              values = [range_to_series("", v) for v in values]
          else:
              # for temporal dtypes:
              # * if the values are integer, we take the physical branch.
              # * if the values are python types, take the temporal branch.
              # * if the values are ISO-8601 strings, init then convert via strptime.
              # * if the values are floats/other dtypes, this is an error.
              if dtype in py_temporal_types and isinstance(value, int):
                  dtype = py_type_to_dtype(
                      dtype
                  )  # construct from integer
              elif (
                  dtype in pl_temporal_types
                  or type(dtype) in pl_temporal_types
              ) and not isinstance(value, int):
                  python_dtype = dtype_to_py_type(dtype)  # type: ignore[arg-type]
  
      # physical branch
      # flat data
      if (
          dtype is not None
          and dtype not in (List, Struct, Unknown)
          and is_polars_dtype(dtype)
          and (python_dtype is None)
      ):
          constructor = polars_type_to_constructor(dtype)
          pyseries = _construct_series_with_fallbacks(
              constructor, name, values, dtype, strict=strict
          )
          if dtype in (
              Date,
              Datetime,
              Duration,
              Time,
              Categorical,
              Boolean,
              Enum,
          ):
              if pyseries.dtype() != dtype:
                  pyseries = pyseries.cast(dtype, strict=True)
          return pyseries
  
      elif dtype == Struct:
          struct_schema = (
              dtype.to_schema() if isinstance(dtype, Struct) else None
          )
          empty = {}  # type: ignore[var-annotated]
          return sequence_to_pydf(
              data=[(empty if v is None else v) for v in values],
              schema=struct_schema,
              orient="row",
          ).to_struct(name)
      else:
          if python_dtype is None:
              if value is None:
                  # Create a series with a dtype_if_empty dtype (if set) or Null
                  # (if not set) for a sequence which contains only None values.
                  constructor = polars_type_to_constructor(
                      dtype_if_empty
                  )
                  return _construct_series_with_fallbacks(
                      constructor, name, values, dtype, strict=strict
                  )
  
              # generic default dtype
              python_dtype = type(value)
  
          # temporal branch
          if python_dtype in py_temporal_types:
              if dtype is None:
                  dtype = py_type_to_dtype(
                      python_dtype
                  )  # construct from integer
              elif dtype in py_temporal_types:
                  dtype = py_type_to_dtype(dtype)
  
              values_dtype = (
                  None
                  if value is None
                  else py_type_to_dtype(
                      type(value), raise_unmatched=False
                  )
              )
              if values_dtype is not None and values_dtype.is_float():
                  raise TypeError(
                      # we do not accept float values as temporal; if this is
                      # required, the caller should explicitly cast to int first.
                      f"'float' object cannot be interpreted as a {python_dtype.__name__!r}"
                  )
  
              # we use anyvalue builder to create the datetime array
              # we store the values internally as UTC and set the timezone
              py_series = PySeries.new_from_anyvalues(
                  name, values, strict
              )
              time_unit = getattr(dtype, "time_unit", None)
              if time_unit is None or values_dtype == Date:
                  s = wrap_s(py_series)
              else:
                  s = wrap_s(py_series).dt.cast_time_unit(time_unit)
              time_zone = getattr(dtype, "time_zone", None)
  
              if (values_dtype == Date) & (dtype == Datetime):
                  return (
                      s.cast(Datetime(time_unit))
                      .dt.replace_time_zone(time_zone)
                      ._s
                  )
  
              if (dtype == Datetime) and (
                  value.tzinfo is not None or time_zone is not None
              ):
                  values_tz = (
                      str(value.tzinfo)
                      if value.tzinfo is not None
                      else None
                  )
                  dtype_tz = dtype.time_zone  # type: ignore[union-attr]
                  if values_tz is not None and (
                      dtype_tz is not None and dtype_tz != "UTC"
                  ):
                      raise ValueError(
                          "time-zone-aware datetimes are converted to UTC"
                          "\n\nPlease either drop the time zone from the dtype, or set it to 'UTC'."
                          " To convert to a different time zone, please use `.dt.convert_time_zone`."
                      )
                  if values_tz != "UTC" and dtype_tz is None:
                      warnings.warn(
                          "Constructing a Series with time-zone-aware "
                          "datetimes results in a Series with UTC time zone. "
                          "To silence this warning, you can filter "
                          "warnings of class TimeZoneAwareConstructorWarning, or "
                          "set 'UTC' as the time zone of your datatype.",
                          TimeZoneAwareConstructorWarning,
                          stacklevel=find_stacklevel(),
                      )
                  return s.dt.replace_time_zone(dtype_tz or "UTC")._s
              return s._s
  
          elif (
              _check_for_numpy(value)
              and isinstance(value, np.ndarray)
              and len(value.shape) == 1
          ):
              n_elems = len(value)
              if all(len(v) == n_elems for v in values):
                  # can take (much) faster path if all lists are the same length
                  return numpy_to_pyseries(
                      name,
                      np.vstack(values),
                      strict=strict,
                      nan_to_null=nan_to_null,
                  )
              else:
                  return PySeries.new_series_list(
                      name,
                      [
                          numpy_to_pyseries(
                              "",
                              v,
                              strict=strict,
                              nan_to_null=nan_to_null,
                          )
                          for v in values
                      ],
                      strict,
                  )
  
          elif python_dtype in (list, tuple):
              if isinstance(dtype, Object):
                  return PySeries.new_object(name, values, strict)
              if dtype:
                  srs = sequence_from_anyvalue_and_dtype_or_object(
                      name, values, dtype
                  )
                  if not dtype.is_(srs.dtype()):
                      srs = srs.cast(dtype, strict=False)
                  return srs
              return sequence_from_anyvalue_or_object(name, values)
  
          elif python_dtype == pl.Series:
              return PySeries.new_series_list(
                  name, [v._s for v in values], strict
              )
  
          elif python_dtype == PySeries:
              return PySeries.new_series_list(name, values, strict)
          else:
              constructor = py_type_to_constructor(python_dtype)
              if constructor == PySeries.new_object:
                  try:
                      srs = PySeries.new_from_anyvalues(
                          name, values, strict
                      )
                      if _check_for_numpy(
                          python_dtype, check_type=False
                      ) and isinstance(np.bool_(True), np.generic):
                          dtype = numpy_char_code_to_dtype(
                              np.dtype(python_dtype).char
                          )
                          return srs.cast(dtype, strict=strict)
                      else:
                          return srs
  
                  except RuntimeError:
                      # raised if we cannot convert to Wrap<AnyValue>
                      return sequence_from_anyvalue_or_object(
                          name, values
                      )
  
              return _construct_series_with_fallbacks(
                  constructor, name, values, dtype, strict=strict
              )
  
imports: from __future__ import annotations
  import contextlib
  import warnings
  from datetime import date, datetime, time, timedelta
  from decimal import Decimal
  from functools import lru_cache, singledispatch
  from itertools import islice, zip_longest
  from operator import itemgetter
  from sys import version_info
  from typing import TYPE_CHECKING, Any, Callable, Generator, Iterable, Iterator, Mapping, MutableMapping, Sequence, get_type_hints
  import polars._reexport
  from polars import functions
  from polars.datatypes import INTEGER_DTYPES, N_INFER_DEFAULT, TEMPORAL_DTYPES, Boolean, Categorical, Date, Datetime, Duration, Enum, List, Null, Object, String, Struct, Time, UInt32, Unknown, dtype_to_py_type, is_polars_dtype, numpy_char_code_to_dtype, py_type_to_dtype
  from polars.datatypes.constructor import numpy_type_to_constructor, numpy_values_and_dtype, polars_type_to_constructor, py_type_to_constructor
  from polars.dependencies import _NUMPY_AVAILABLE, _check_for_numpy, _check_for_pandas, _check_for_pydantic, dataclasses, pydantic
  from polars.dependencies import numpy
  from polars.dependencies import pandas
  from polars.dependencies import pyarrow
  from polars.exceptions import ComputeError, ShapeError, TimeZoneAwareConstructorWarning
  from polars.utils._wrap import wrap_df, wrap_s
  from polars.utils.meta import get_index_type, threadpool_size
  from polars.utils.various import _is_generator, arrlen, find_stacklevel, parse_version, range_to_series
label_desc: Use image node_id 15 for calling a global function with example usage: sequence_to_pyseries(name, values, dtype) and returns: pyseries, pl, sequence_to_pydf, s, _construct_series_with_fallbacks, s, s, numpy_to_pyseries, PySeries, sequence_from_anyvalue_or_object, PySeries, srs, PySeries, PySeries, _construct_series_with_fallbacks, srs, srs, sequence_from_anyvalue_or_object