code: def test_stationarity(
      time_df,
      maxlag=31,
      regression="c",
      autolag="BIC",
      window=None,
      plot=False,
      verbose=False,
      var_only=False,
  ):
      """
      Check unit root stationarity of a time series array or an entire dataframe.
      Note that you must send in a dataframe as df.values.ravel() - otherwise ERROR.
      Null hypothesis: the series is non-stationary.
      If p >= alpha, the series is non-stationary.
      If p < alpha, reject the null hypothesis (has unit root stationarity).
      Original source: http://www.analyticsvidhya.com/blog/2016/02/time-series-forecasting-codes-python/
      Function: http://statsmodels.sourceforge.net/devel/generated/statsmodels.tsa.stattools.adfuller.html
      window argument is only required for plotting rolling functions. Default=4.
      """
      time_df = copy.deepcopy(time_df)
      if len(time_df) <= int(2.5 * maxlag):
          maxlag = 5
          print("setting maxlag to a low number = %s" % maxlag)
      # set defaults (from function page)
      if type(time_df) == pd.DataFrame:
          # print('modifying time series dataframe into an array to test')
          timeseries = time_df.values.ravel()
      else:
          timeseries = copy.deepcopy(time_df)
      if regression is None:
          regression = "c"
      if verbose:
          print(
              "\nRunning Augmented Dickey-Fuller test with paramters:"
          )
          print(
              "    maxlag: {}".format(maxlag),
              "regression: {}".format(regression),
              "autolag: {}".format(autolag),
          )
      alpha = 0.05
      if plot:
          try:
              if window is None:
                  window = 4
              # Determing rolling statistics
              rolmean = timeseries.rolling(
                  window=window, center=False
              ).mean()
              rolstd = timeseries.rolling(
                  window=window, center=False
              ).std()
              # Plot rolling statistics:
              orig = plt.plot(
                  timeseries, color="blue", label="Original"
              )
              mean = plt.plot(
                  rolmean,
                  color="red",
                  label="Rolling Mean ({})".format(window),
              )
              std = plt.plot(
                  rolstd,
                  color="black",
                  label="Rolling Std ({})".format(window),
              )
              plt.legend(loc="best")
              plt.title("Rolling Mean & Standard Deviation")
              plt.show(block=False)
          except:
              print("Data must have date-time as index to plot!")
              return
      # Perform Augmented Dickey-Fuller test:
      if var_only:
          ### In VAR models, check all_vars for stationarity
          ### if it is 1, then all vars are stationary. If not difference it once and try again!
          ### Use Statsmodels for tests ###########
          diff_limit = 0
          for i in range(3):
              stationary_test = check_each_var_for_stationarity(
                  time_df, autolag, verbose
              )
              if stationary_test:
                  if i == 0:
                      print("Data is already stationary")
                      diff_limit = 0
                      break
                  elif i == 1:
                      print("Data is stationary after one differencing")
                      diff_limit = 1
                      break
                  elif i == 2:
                      diff_limit = 2
                      print("Data is stationary after two differencing")
                      break
              else:
                  if i == 2:
                      print(
                          "Alert! Data is not stationary even after two differencing. Continuing..."
                      )
                      diff_limit = 0
                      break
                  else:
                      time_df = time_df.diff(1).dropna()
                      continue
          return diff_limit
      else:
          ### In non-VAR models you need to test only the target variable for stationarity ##
          dftest = smt.adfuller(
              timeseries,
              maxlag=maxlag,
              regression=regression,
              autolag=autolag,
          )
          dfoutput = pd.Series(
              dftest[0:4],
              index=[
                  "Test Statistic",
                  "p-value",
                  "#Lags Used",
                  "Number of Observations Used",
              ],
              name="Dickey-Fuller Augmented Test",
          )
          for key, value in dftest[4].items():
              dfoutput["Critical Value (%s)" % key] = value
          if verbose:
              print("Results of Augmented Dickey-Fuller Test:")
              pretty_print_table(dfoutput)
          if dftest[1] >= alpha:
              print(
                  " this series is non-stationary. Trying test again after differencing..."
              )
              timeseries = pd.Series(timeseries).diff(1).dropna().values
              dftest = smt.adfuller(
                  timeseries,
                  maxlag=maxlag,
                  regression=regression,
                  autolag=autolag,
              )
              dfoutput = pd.Series(
                  dftest[0:4],
                  index=[
                      "Test Statistic",
                      "p-value",
                      "#Lags Used",
                      "Number of Observations Used",
                  ],
                  name="Dickey-Fuller Augmented Test",
              )
              for key, value in dftest[4].items():
                  dfoutput["Critical Value (%s)" % key] = value
              if verbose:
                  print(
                      "After differencing=1, results of Augmented Dickey-Fuller Test:"
                  )
                  pretty_print_table(dfoutput)
              if dftest[1] >= alpha:
                  print(
                      colorful.BOLD
                      + "this series is NOT stationary"
                      + colorful.END
                  )
                  return False
              else:
                  print(
                      colorful.BOLD
                      + "this series is stationary"
                      + colorful.END
                  )
                  return True
          else:
              print(
                  colorful.BOLD
                  + "this series is stationary"
                  + colorful.END
              )
              return True
  
imports: from itertools import cycle
  import numpy
  import pandas
  import matplotlib.dates
  from itertools import cycle
  import matplotlib.pyplot
  import seaborn
  import statsmodels.tsa.api
  import pdb
  import copy
  import dask
  import dask.dataframe
  from .colors import colorful
  import copy
label_desc: Use image node_id 5 for calling a global function with example usage: test_stationarity(time_df, maxlag, regression, autolag, window, plot, verbose, var_only) and returns: diff_limit, True, False, True