code: def detrend(x, order=1, axis=0):
      """
      Detrend an array with a trend of given order along axis 0 or 1.
  
      Parameters
      ----------
      x : array_like, 1d or 2d
          Data, if 2d, then each row or column is independently detrended with
          the same trendorder, but independent trend estimates.
      order : int
          The polynomial order of the trend, zero is constant, one is
          linear trend, two is quadratic trend.
      axis : int
          Axis can be either 0, observations by rows, or 1, observations by
          columns.
  
      Returns
      -------
      ndarray
          The detrended series is the residual of the linear regression of the
          data on the trend of given order.
      """
      order = int_like(order, "order")
      axis = int_like(axis, "axis")
  
      if x.ndim == 2 and int(axis) == 1:
          x = x.T
      elif x.ndim > 2:
          raise NotImplementedError(
              "x.ndim > 2 is not implemented until it is needed"
          )
  
      nobs = x.shape[0]
      if order == 0:
          # Special case demean
          resid = x - x.mean(axis=0)
      else:
          trends = np.vander(np.arange(float(nobs)), N=order + 1)
          beta = np.linalg.pinv(trends).dot(x)
          resid = x - np.dot(trends, beta)
  
      if x.ndim == 2 and int(axis) == 1:
          resid = resid.T
  
      return resid
  
imports: from __future__ import annotations
  from statsmodels.compat.python import Literal, lrange
  import warnings
  import numpy
  import pandas
  from pandas import DataFrame
  from pandas.tseries import offsets
  from pandas.tseries.frequencies import to_offset
  from statsmodels.tools.data import _is_recarray, _is_using_pandas
  from statsmodels.tools.sm_exceptions import ValueWarning
  from statsmodels.tools.typing import NDArray
  from statsmodels.tools.validation import array_like, bool_like, int_like, string_like
label_desc: Use image node_id 3 for calling a global function with example usage: detrend(x, order, axis) and returns: resid