parameters: (
      values: Union[np.ndarray, Index],
      tz: Optional[Union[str, tzinfo]],
      coerce: bool = False,
  )
return_statement: return values
docstring: coerce the values to a DatetimeIndex if tz is set
      preserve the input shape if possible
  
      Parameters
      ----------
      values : ndarray or Index
      tz : str or tzinfo
      coerce : if we do not have a passed timezone, coerce to M8[ns] ndarray
docstring_summary: coerce the values to a DatetimeIndex if tz is set
      preserve the input shape if possible
docstring_tokens: coerce the values to a DatetimeIndex if tz is set preserve the input shape if possible
function: def _set_tz(
      values: Union[np.ndarray, Index],
      tz: Optional[Union[str, tzinfo]],
      coerce: bool = False,
  ) -> Union[np.ndarray, DatetimeIndex]:
      """
      coerce the values to a DatetimeIndex if tz is set
      preserve the input shape if possible
  
      Parameters
      ----------
      values : ndarray or Index
      tz : str or tzinfo
      coerce : if we do not have a passed timezone, coerce to M8[ns] ndarray
      """
      if isinstance(values, DatetimeIndex):
          # If values is tzaware, the tz gets dropped in the values.ravel()
          #  call below (which returns an ndarray).  So we are only non-lossy
          #  if `tz` matches `values.tz`.
          assert values.tz is None or values.tz == tz
  
      if tz is not None:
          name = getattr(values, "name", None)
          values = values.ravel()
          tz = timezones.get_timezone(_ensure_decoded(tz))
          values = DatetimeIndex(values, name=name)
          values = values.tz_localize("UTC").tz_convert(tz)
      elif coerce:
          values = np.asarray(values, dtype="M8[ns]")
  
      return values
function_tokens: def _set_tz ( values : Union [ np . ndarray , Index ] , tz : Optional [ Union [ str , tzinfo ] ] , coerce : bool = False , ) -> Union [ np . ndarray , DatetimeIndex ] : if isinstance ( values , DatetimeIndex ) : # If values is tzaware, the tz gets dropped in the values.ravel() #  call below (which returns an ndarray).  So we are only non-lossy #  if `tz` matches `values.tz`. assert values . tz is None or values . tz == tz if tz is not None : name = getattr ( values , "name" , None ) values = values . ravel ( ) tz = timezones . get_timezone ( _ensure_decoded ( tz ) ) values = DatetimeIndex ( values , name = name ) values = values . tz_localize ( "UTC" ) . tz_convert ( tz ) elif coerce : values = np . asarray ( values , dtype = "M8[ns]" ) return values