def sindexf(decomp, h):
  '''
  Projects the seasonal component of a seasonal decomposition of a time series 
  forward by h time steps into the future.
  
  Args:
    decomp: an R seasonal decomposition from stl or decompose,
      or a Pandas data frame containing a seasonal decomposition.
    h: a forecast horizon
    
  Returns:
    an object that maps to am R time series containing the seasonal component 
    of decomp, projected naively forward h steps.
  '''
  if validate.is_Pandas_decomposition(decomp):
    freq = len(decomp.index.levels[1])
    seasonal = list(decomp.seasonal[-freq:])
    it = itertools.cycle(seasonal)
    data = []
    for k in range(h):
      data.append(it.next())
    data = [0] + data
    start = decomp.seasonal.last_valid_index()
    series = converters.sequence_as_series(data, start=start, freq=freq)
    return series[1:]
  elif validate.is_R_decomposition(decomp):
    return fc.sindexf(decomp, h)
  else:
    raise ValueError('seasadj requires a seasonal decomposition as input')
  def test_sequence_as_series(self):
    loil = list(self.oil_ts)
    laus = list(self.aus_ts)
    oil = converters.sequence_as_series(loil, start=1965)
    aus = converters.sequence_as_series(laus, start=(1999, 1), freq=4)
    self.assertEqual(oil.shape, (46,))
    self.assertEqual(oil.index.nlevels, 1)
    self.assertEqual(oil.index[0], 1965)
    self.assertEqual(oil.index[-1], 2010)
    self.assertAlmostEqual(oil[1965], 111.00, places=1)
    self.assertAlmostEqual(oil[2010], 467.77, places=1)
    
    self.assertEqual(aus.shape, (48, ))
    self.assertEqual(aus.index.nlevels, 2)
    self.assertEqual(aus.index[0], (1999, 1))
    self.assertEqual(aus.index[-1], (2010, 4))
    self.assertEqual(aus[2010].shape, (4,))
    self.assertAlmostEqual(aus[(1999, 1)], 30.05, places=1)
    self.assertAlmostEqual(aus[(2010, 4)], 47.91, places=1)

    aus2 = converters.sequence_as_series(laus, start=1999, freq=4)
    self.assertTrue(aus2.equals(aus))
 def setUp(self):
   self.oil = ts_io.read_ts('oil', 'fpp', False)
   self.aus = ts_io.read_ts('austourists', 'fpp', False)
   self.gold = ts_io.read_ts('gold', as_pandas=False)
   self.tsn = converters.ts([1, 2, NA, 4])
   self.tss = converters.ts([1, 2, 3, 1, 2, 3, 1, NA, 3], frequency=3)
   self.vss = [1,2,3,4] * 4
   self.vns = range(10)
   r = [ 0.00287731,  0.58436909,  0.37650672,  0.10024602,  0.46983146,
       0.36542408,  0.47136475,  0.79978803,  0.70349953,  0.69531808,
       0.54447409,  0.82227504,  0.99736304,  0.91404314,  0.42225177,
       0.14696605,  0.08098318,  0.11046747,  0.8412757 ,  0.73562921]
   self.rnd = converters.sequence_as_series(r, freq=4)
    def test_sequence_as_series(self):
        loil = list(self.oil_ts)
        laus = list(self.aus_ts)
        oil = converters.sequence_as_series(loil, start=1965)
        aus = converters.sequence_as_series(laus, start=(1999, 1), freq=4)
        self.assertEqual(oil.shape, (46, ))
        self.assertEqual(oil.index.nlevels, 1)
        self.assertEqual(oil.index[0], 1965)
        self.assertEqual(oil.index[-1], 2010)
        self.assertAlmostEqual(oil[1965], 111.00, places=1)
        self.assertAlmostEqual(oil[2010], 467.77, places=1)

        self.assertEqual(aus.shape, (48, ))
        self.assertEqual(aus.index.nlevels, 2)
        self.assertEqual(aus.index[0], (1999, 1))
        self.assertEqual(aus.index[-1], (2010, 4))
        self.assertEqual(aus[2010].shape, (4, ))
        self.assertAlmostEqual(aus[(1999, 1)], 30.05, places=1)
        self.assertAlmostEqual(aus[(2010, 4)], 47.91, places=1)

        aus2 = converters.sequence_as_series(laus, start=1999, freq=4)
        self.assertTrue(aus2.equals(aus))
 def setUp(self):
     self.oil = ts_io.read_ts('oil', 'fpp', False)
     self.aus = ts_io.read_ts('austourists', 'fpp', False)
     self.gold = ts_io.read_ts('gold', as_pandas=False)
     self.tsn = converters.ts([1, 2, NA, 4])
     self.tss = converters.ts([1, 2, 3, 1, 2, 3, 1, NA, 3], frequency=3)
     self.vss = [1, 2, 3, 4] * 4
     self.vns = range(10)
     r = [
         0.00287731, 0.58436909, 0.37650672, 0.10024602, 0.46983146,
         0.36542408, 0.47136475, 0.79978803, 0.70349953, 0.69531808,
         0.54447409, 0.82227504, 0.99736304, 0.91404314, 0.42225177,
         0.14696605, 0.08098318, 0.11046747, 0.8412757, 0.73562921
     ]
     self.rnd = converters.sequence_as_series(r, freq=4)
     self.fc = importr('forecast')