code: def test_datetime_attribute_timeseries(self):
      idx = generate_index(
          start=pd.Timestamp("2000-01-01"), length=48, freq="h"
      )
  
      def helper_routine(idx, attr, vals_exp, **kwargs):
          ts = datetime_attribute_timeseries(
              idx, attribute=attr, **kwargs
          )
          vals_exp = np.array(vals_exp, dtype=ts.dtype)
          if len(vals_exp.shape) == 1:
              vals_act = ts.values()[:, 0]
          else:
              vals_act = ts.values()
          np.testing.assert_array_almost_equal(vals_act, vals_exp)
  
      # no pd.DatetimeIndex
      with pytest.raises(ValueError) as err:
          helper_routine(
              pd.RangeIndex(start=0, stop=len(idx)),
              "h",
              vals_exp=np.arange(len(idx)),
          )
      assert str(err.value).startswith(
          "`time_index` must be a pandas `DatetimeIndex`"
      )
  
      # invalid attribute
      with pytest.raises(ValueError) as err:
          helper_routine(idx, "h", vals_exp=np.arange(len(idx)))
      assert str(err.value).startswith(
          "attribute `h` needs to be an attribute of pd.DatetimeIndex."
      )
  
      # no time zone aware index
      with pytest.raises(ValueError) as err:
          helper_routine(
              idx.tz_localize("UTC"), "h", vals_exp=np.arange(len(idx))
          )
      assert "`time_index` must be time zone naive." == str(err.value)
  
      # ===> datetime attribute
      # hour
      vals = [i for i in range(24)] * 2
      helper_routine(idx, "hour", vals_exp=vals)
  
      # hour from TimeSeries
      helper_routine(
          TimeSeries.from_times_and_values(
              times=idx, values=np.arange(len(idx))
          ),
          "hour",
          vals_exp=vals,
      )
  
      # tz=CET is +1 hour to UTC
      vals = vals[1:] + [0]
      helper_routine(idx, "hour", vals_exp=vals, tz="CET")
  
      # day
      vals = [1] * 24 + [2] * 24
      helper_routine(idx, "day", vals_exp=vals)
  
      # dayofweek
      vals = [5] * 24 + [6] * 24
      helper_routine(idx, "dayofweek", vals_exp=vals)
  
      # month
      vals = [1] * 48
      helper_routine(idx, "month", vals_exp=vals)
  
      # ===> one hot encoded
      # month
      vals = [1] + [0] * 11
      vals = [vals for _ in range(48)]
      helper_routine(idx, "month", vals_exp=vals, one_hot=True)
  
      # tz=CET, month
      vals = [1] + [0] * 11
      vals = [vals for _ in range(48)]
      helper_routine(
          idx, "month", vals_exp=vals, tz="CET", one_hot=True
      )
  
      # ===> sine/cosine cyclic encoding
      # hour (period = 24 hours in one day)
      period = 24
      freq = 2 * np.pi / period
      vals_dta = [i for i in range(24)] * 2
      vals = np.array(vals_dta)
      sin_vals = np.sin(freq * vals)[:, None]
      cos_vals = np.cos(freq * vals)[:, None]
      vals = np.concatenate([sin_vals, cos_vals], axis=1)
      helper_routine(idx, "hour", vals_exp=vals, cyclic=True)
  
      # tz=CET, hour
      vals = np.array(vals_dta[1:] + [0])
      sin_vals = np.sin(freq * vals)[:, None]
      cos_vals = np.cos(freq * vals)[:, None]
      vals = np.concatenate([sin_vals, cos_vals], axis=1)
      helper_routine(idx, "hour", vals_exp=vals, tz="CET", cyclic=True)
  
file_path: darts/darts/tests/utils/test_timeseries_generation.py
imports: from typing import Union
  import numpy
  import pandas
  import pytest
  from darts import TimeSeries
  from darts.utils.timeseries_generation import autoregressive_timeseries, constant_timeseries, datetime_attribute_timeseries, gaussian_timeseries, generate_index, holidays_timeseries, linear_timeseries, random_walk_timeseries, sine_timeseries
label_desc: Use image node_id 9 for calling the TestTimeSeriesGeneration obj's underlying member method code with example usage: obj.test_datetime_attribute_timeseries() without return types