class_docstr: A class to infer appropriate Fourier series orders in different
  seasonality components.
  
  The method allows users to:
  
      - optionally remove the trend with different methods.
        Available methods are in `TrendAdjustMethodEnum`.
      - optionally do an aggregation.
      - fits the seasonality component with different Fourier series orders.
      - calculates the AIC/BIC of the fits.
      - choose the most appropriate order with AIC or BIC and an optional tolerance.
      - plot the investigations.
  
  Attributes
  ----------
  df : `pandas.DataFrame` or None
      The input timeseries.
  time_col : `str` or None
      The column name for timestamps in ``df``.
  value_col : `str` or None
      The column name for values in ``df``.
  fourier_series_orders : `dict` or None
      The inferred Fourier series orders.
      The keys are the seasonality component names.
      The values are the inferred best orders according to the config.
  df_features : `pandas.DataFrame` or None
      The cached dataframe with time features.
      Building this df is slow for large dataset.
      We cache it the first time we build it for subsequent uses.
docstr: Adjusts the time series by removing trend.
  
  There methods to remove trend from time series are listed in
  `~greykite.algo.common.seasonality_inferrer.TrendAdjustMethodEnum`.
  The methods are:
  
      - "seasonal_average" : subtracts the average from each seasonal period.
      - "overall_average" : subtracts the overall average.
      - "spline_fit" : fits the trend using a spline with no knots up to a certain degree (poly).
  
  Parameters
  ----------
  df : `pandas.DataFrame`
      The input time series.
  time_col : `str`, default ``TIME_COL``
      The column name for timestamps in ``df``.
  value_col : `str`, default ``VALUE_COL``
      The column name for values in ``df``.
  method : `str`, default `~greykite.algo.common.seasonality_inferrer.TrendAdjustMethodEnum.seasonal_average.name`
      The adjustment method. Must be a method specified in
      `~greykite.algo.common.seasonality_inferrer.TrendAdjustMethodEnum`.
  trend_average_col : `str`, default "year"
      The column name that specifies the categories of calculating seasonal mean.
      Suggested columns are "year", "year_quarter", "year_month", etc.
      Has no effect if ``method`` is not
      `~greykite.algo.common.seasonality_inferrer.TrendAdjustMethodEnum.seasonal_average.name`.
      Must be provided in ``df`` or be generated by
      `~greykite.common.features.timeseries_features import build_time_features_df`.
  spline_fit_degree : `int`, default 1
      The degree of spline to fit the trend.
      Has no effect if ``method`` is not
      `~greykite.algo.common.seasonality_inferrer.TrendAdjustMethodEnum.spline_fit.name`.
  
  Returns
  -------
  df : `pandas.DataFrame`
      A copy of the original df but with the ``value_col`` subtracting trend
      and an extra column indicating the fitted trend.
      The ``value_col`` will have mean zero if trend is adjusted.
docstr_tok: ["Adjusts","the","time","series","by","removing","trend",".","There","methods","to","remove","trend","from","time","series","are","listed","in","`","~greykite.algo.common.seasonality_inferrer.TrendAdjustMethodEnum","`",".","The","methods","are",":","-","``","seasonal_average","''",":","subtracts","the","average","from","each","seasonal","period",".","-","``","overall_average","''",":","subtracts","the","overall","average",".","-","``","spline_fit","''",":","fits","the","trend","using","a","spline","with","no","knots","up","to","a","certain","degree","(","poly",")",".","Parameters","--","--","--","--","--","df",":","`","pandas.DataFrame","`","The","input","time","series",".","time_col",":","`","str","`",",","default","``","TIME_COL","``","The","column","name","for","timestamps","in","``","df","``",".","value_col",":","`","str","`",",","default","``","VALUE_COL","``","The","column","name","for","values","in","``","df","``",".","method",":","`","str","`",",","default","`","~greykite.algo.common.seasonality_inferrer.TrendAdjustMethodEnum.seasonal_average.name","`","The","adjustment","method",".","Must","be","a","method","specified","in","`","~greykite.algo.common.seasonality_inferrer.TrendAdjustMethodEnum","`",".","trend_average_col",":","`","str","`",",","default","``","year","''","The","column","name","that","specifies","the","categories","of","calculating","seasonal","mean",".","Suggested","columns","are","``","year","''",",","``","year_quarter","''",",","``","year_month","''",",","etc",".","Has","no","effect","if","``","method","``","is","not","`","~greykite.algo.common.seasonality_inferrer.TrendAdjustMethodEnum.seasonal_average.name","`",".","Must","be","provided","in","``","df","``","or","be","generated","by","`","~greykite.common.features.timeseries_features","import","build_time_features_df","`",".","spline_fit_degree",":","`","int","`",",","default","1","The","degree","of","spline","to","fit","the","trend",".","Has","no","effect","if","``","method","``","is","not","`","~greykite.algo.common.seasonality_inferrer.TrendAdjustMethodEnum.spline_fit.name","`",".","Returns","--","--","--","-","df",":","`","pandas.DataFrame","`","A","copy","of","the","original","df","but","with","the","``","value_col","``","subtracting","trend","and","an","extra","column","indicating","the","fitted","trend",".","The","``","value_col","``","will","have","mean","zero","if","trend","is","adjusted","."]
code: def _adjust_trend(
      self,
      df: pd.DataFrame,
      time_col: str = TIME_COL,
      value_col: str = VALUE_COL,
      method: str = TrendAdjustMethodEnum.seasonal_average.name,
      trend_average_col: str = "year",
      spline_fit_degree: int = 1,
  ) -> pd.DataFrame:
      """Adjusts the time series by removing trend.
  
      There methods to remove trend from time series are listed in
      `~greykite.algo.common.seasonality_inferrer.TrendAdjustMethodEnum`.
      The methods are:
  
          - "seasonal_average" : subtracts the average from each seasonal period.
          - "overall_average" : subtracts the overall average.
          - "spline_fit" : fits the trend using a spline with no knots up to a certain degree (poly).
  
      Parameters
      ----------
      df : `pandas.DataFrame`
          The input time series.
      time_col : `str`, default ``TIME_COL``
          The column name for timestamps in ``df``.
      value_col : `str`, default ``VALUE_COL``
          The column name for values in ``df``.
      method : `str`, default `~greykite.algo.common.seasonality_inferrer.TrendAdjustMethodEnum.seasonal_average.name`
          The adjustment method. Must be a method specified in
          `~greykite.algo.common.seasonality_inferrer.TrendAdjustMethodEnum`.
      trend_average_col : `str`, default "year"
          The column name that specifies the categories of calculating seasonal mean.
          Suggested columns are "year", "year_quarter", "year_month", etc.
          Has no effect if ``method`` is not
          `~greykite.algo.common.seasonality_inferrer.TrendAdjustMethodEnum.seasonal_average.name`.
          Must be provided in ``df`` or be generated by
          `~greykite.common.features.timeseries_features import build_time_features_df`.
      spline_fit_degree : `int`, default 1
          The degree of spline to fit the trend.
          Has no effect if ``method`` is not
          `~greykite.algo.common.seasonality_inferrer.TrendAdjustMethodEnum.spline_fit.name`.
  
      Returns
      -------
      df : `pandas.DataFrame`
          A copy of the original df but with the ``value_col`` subtracting trend
          and an extra column indicating the fitted trend.
          The ``value_col`` will have mean zero if trend is adjusted.
      """
      # Checks if the method is valid.
      if method not in TrendAdjustMethodEnum.__dict__["_member_names_"]:
          raise ValueError(
              f"The trend adjust method '{method}' is not a valid name. "
              f"Available methods are {TrendAdjustMethodEnum.__dict__['_member_names_']}."
          )
      df = df.copy()
      # Subtracts the overall time series average.
      if method == TrendAdjustMethodEnum.overall_average.name:
          df[self.FITTED_TREND_COL] = df[value_col].mean()
  
      # Subtracts the seasonal average within each seasonal period.
      if method == TrendAdjustMethodEnum.seasonal_average.name:
          df_cols = list(df.columns)
          if trend_average_col not in df.columns:
              if (
                  trend_average_col
                  in TimeFeaturesEnum.__dict__["_member_names_"]
              ):
                  if self.df_features is None:
                      df = add_time_features_df(
                          df=df,
                          time_col=time_col,
                          conti_year_origin=get_default_origin_for_time_vars(
                              df=df, time_col=time_col
                          ),
                      )
                      self.df_features = df.copy()
                  else:
                      df = self.df_features.copy()
              else:
                  raise ValueError(
                      f"The trend_average_col '{trend_average_col}' is neither found in df "
                      f"'{list(df.columns)}' nor in the built time features "
                      f"'{TimeFeaturesEnum.__dict__['_member_names_']}'."
                  )
          df_seasonal_average = (
              df[[value_col, trend_average_col]]
              .groupby(trend_average_col)
              .mean()
              .reset_index(drop=False)
              .rename(columns={value_col: self.FITTED_TREND_COL})
          )
          df = df.merge(df_seasonal_average, on=trend_average_col)
          df = df[df_cols + [self.FITTED_TREND_COL]]
  
      # Subtracts the fitted trend.
      if method == TrendAdjustMethodEnum.spline_fit.name:
          if spline_fit_degree < 1:
              raise ValueError(
                  f"Spline degree has be to a positive integer, "
                  f"but found {spline_fit_degree}."
              )
          df_features = add_time_features_df(
              df=df,
              time_col=time_col,
              conti_year_origin=get_default_origin_for_time_vars(
                  df=df, time_col=time_col
              ),
          )
          df_fit = df_features[[value_col, TimeFeaturesEnum.ct1.name]]
          fit_cols = [TimeFeaturesEnum.ct1.name]
          for degree in range(2, int(spline_fit_degree) + 1):
              df_fit[f"ct{degree}"] = (
                  df_fit[TimeFeaturesEnum.ct1.name] ** degree
              )
              fit_cols.append(f"ct{degree}")
          model = SGDRegressor()
          model.fit(df_fit[fit_cols], df_fit[value_col])
          df[self.FITTED_TREND_COL] = model.predict(df_fit[fit_cols])
  
      df[value_col] -= df[self.FITTED_TREND_COL]
      return df
  
imports: from dataclasses import dataclass
  from enum import Enum
  from typing import List
  from typing import Optional
  import numpy
  import pandas
  from plotly import graph_objs
  from plotly.colors import DEFAULT_PLOTLY_COLORS
  from plotly.subplots import make_subplots
  from sklearn.linear_model import LinearRegression
  from sklearn.linear_model import RidgeCV
  from sklearn.linear_model import SGDRegressor
  from greykite.common.constants import TIME_COL
  from greykite.common.constants import VALUE_COL
  from greykite.common.constants import TimeFeaturesEnum
  from greykite.common.features.timeseries_features import add_time_features_df
  from greykite.common.features.timeseries_features import fourier_series_multi_fcn
  from greykite.common.features.timeseries_features import get_default_origin_for_time_vars
label_desc: Use image node_id 5 for calling the SeasonalityInferrer obj's underlying member method code with example usage: obj._adjust_trend(df, time_col, value_col, method, trend_average_col, spline_fit_degree) and returns: df