def InvBoxCox(x, lam):
    '''
  Invert a BoxCox transformation. The return value is a timeseries with 
  values of x transformed back to the original scale
  
  Args:
    x: an R time series, obtained from converters.ts(), or a Pandas Series
      with the correct index (e.g. from converters.sequence_as_series().
      Its values that should be on the scale of a BoxCox transformation 
      with parameter lambda=lam.
    lam: BoxCox transformation parameter. 
      
  Returns:
    If x is an R ts object, an R time series is returned. 
    If x is a Pandas Series, a Pandas Series is returned.
  '''
    x, is_pandas = converters.to_ts(x)
    out = fc.InvBoxCox(x, **{'lambda': lam})
    return converters.series_out(out, is_pandas)
def InvBoxCox(x, lam):
  '''
  Invert a BoxCox transformation. The return value is a timeseries with 
  values of x transformed back to the original scale
  
  Args:
    x: an R time series, obtained from converters.ts(), or a Pandas Series
      with the correct index (e.g. from converters.sequence_as_series().
      Its values that should be on the scale of a BoxCox transformation 
      with parameter lambda=lam.
    lam: BoxCox transformation parameter. 
      
  Returns:
    If x is an R ts object, an R time series is returned. 
    If x is a Pandas Series, a Pandas Series is returned.
  '''
  x, is_pandas = converters.to_ts(x)
  out = fc.InvBoxCox(x, **{'lambda' : lam})
  return converters.series_out(out, is_pandas)
def BoxCox(x, lam):
    '''
  Applies a Box-Cox transformation to the data in x. This can stabilize the 
  variance of x, so that forecast model assumptions are more nearly satisfied.
  
  For x != 0, this is (x^lambda - 1) / lambda.
  For x = 0, it is log(x).
  
  Args:
    x: an R time series, obtained from converters.ts(), or a Pandas Series
      with the correct index (e.g. from converters.sequence_as_series().
    lam: BoxCox transformation parameter. 
      
  Returns:
    If x is an R ts object, an R time series is returned. 
    If x is a Pandas Series, a Pandas Series is returned.
  '''
    x, is_pandas = converters.to_ts(x)
    out = fc.BoxCox(x, **{'lambda': lam})
    return converters.series_out(out, is_pandas)
def BoxCox(x, lam):
  '''
  Applies a Box-Cox transformation to the data in x. This can stabilize the 
  variance of x, so that forecast model assumptions are more nearly satisfied.
  
  For x != 0, this is (x^lambda - 1) / lambda.
  For x = 0, it is log(x).
  
  Args:
    x: an R time series, obtained from converters.ts(), or a Pandas Series
      with the correct index (e.g. from converters.sequence_as_series().
    lam: BoxCox transformation parameter. 
      
  Returns:
    If x is an R ts object, an R time series is returned. 
    If x is a Pandas Series, a Pandas Series is returned.
  '''
  x, is_pandas = converters.to_ts(x)
  out = fc.BoxCox(x, **{'lambda' : lam})
  return converters.series_out(out, is_pandas)
def tsclean(x, **kwargs):
    '''
  Identify and replace outliers. Uses loess for non-seasonal series and 
  an STL decomposition for seasonal series. Optionally fills missing values.

  Args:
    x: an R time series, obtained from converters.ts(), or a Pandas Series
      with the correct index (e.g. from converters.sequence_as_series().
    replace_missing: Default True. 
      If True, use na_interp to fill missing values in x.
    lam: optional BoxCox transformation parameter.
    
  Returns:
    If x is an R ts object, an R time series is returned. If x is a Pandas 
    Series, a Pandas Series is returned. In either case, outliers are replaced 
    and optionally, missing values are filled.
  '''
    x, is_pandas = converters.to_ts(x)
    kwargs = converters.translate_kwargs(**kwargs)
    out = fc.tsclean(x, **kwargs)
    return converters.series_out(out, is_pandas)
def tsclean(x, **kwargs):
  '''
  Identify and replace outliers. Uses loess for non-seasonal series and 
  an STL decomposition for seasonal series. Optionally fills missing values.

  Args:
    x: an R time series, obtained from converters.ts(), or a Pandas Series
      with the correct index (e.g. from converters.sequence_as_series().
    replace_missing: Default True. 
      If True, use na_interp to fill missing values in x.
    lam: optional BoxCox transformation parameter.
    
  Returns:
    If x is an R ts object, an R time series is returned. If x is a Pandas 
    Series, a Pandas Series is returned. In either case, outliers are replaced 
    and optionally, missing values are filled.
  '''
  x, is_pandas = converters.to_ts(x)
  kwargs = converters.translate_kwargs(**kwargs)
  out = fc.tsclean(x, **kwargs)
  return converters.series_out(out, is_pandas)
def na_interp(x, lam=NULL):
    '''
  Funtction for interpolating missing values in R time series. This function 
  uses linear interpolation for non-seasonal data. For seasonal data, it 
  uses an STL decomposition, imputing the seasonal value.
  
  Args:
    x: an R time series, obtained from converters.ts(), or a Pandas Series
      with the correct index (e.g. from converters.sequence_as_series().
      If lam is used, its values should be on the scale of a BoxCox
      transformation with parameter lambda=lam.
    lam: BoxCox transformation parameter. The default is R's NULL value.
      If NULL, no transformation is applied. Otherwise, a Box-Cox 
      transformation is applied before forecasting and inverted after.  
      
  Returns:
    If x is an R ts object, an R time series is returned. 
    If x is a Pandas Series, a Pandas Series is returned.
    In either case, missing values are filled.
  '''
    x, is_pandas = converters.to_ts(x)
    out = fc.na_interp(x, **{'lambda': lam})
    return converters.series_out(out, is_pandas)
def na_interp(x, lam=NULL):
  '''
  Funtction for interpolating missing values in R time series. This function 
  uses linear interpolation for non-seasonal data. For seasonal data, it 
  uses an STL decomposition, imputing the seasonal value.
  
  Args:
    x: an R time series, obtained from converters.ts(), or a Pandas Series
      with the correct index (e.g. from converters.sequence_as_series().
      If lam is used, its values should be on the scale of a BoxCox
      transformation with parameter lambda=lam.
    lam: BoxCox transformation parameter. The default is R's NULL value.
      If NULL, no transformation is applied. Otherwise, a Box-Cox 
      transformation is applied before forecasting and inverted after.  
      
  Returns:
    If x is an R ts object, an R time series is returned. 
    If x is a Pandas Series, a Pandas Series is returned.
    In either case, missing values are filled.
  '''
  x, is_pandas = converters.to_ts(x)
  out = fc.na_interp(x, **{'lambda' : lam})
  return converters.series_out(out, is_pandas)
def read_ts(ts_name, pkgname=None, as_pandas=True):
  '''
  Function reads a time series in from R. If needed, it can load a package 
  containing the time series. The output can be provided as an R object or 
  as a Pandas Series.
  
  Args:
    ts_name: the name of the time series in R
    pkgname: Default None. The name of an R package with the time series.
    as_pandas: Default True. If true, return a Pandas Series.

  Returns:
    the time series as an R time series or a Pandas Series
  '''
  if pkgname is not None:
    try:
      importr(pkgname)
    except RRuntimeError:
      raise IOError('Package %s not found in R.' % pkgname)
  try:
    tsout = robjects.r(ts_name)
  except RRuntimeError:
    raise IOError('Time series %s not found in R.' % ts_name)
  return converters.series_out(tsout, as_pandas)