def detrend(x, order=1, axis=0):
 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):
  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

def detrend ( x , order = 1 , axis = 0 ) : 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 ) : 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
detrend the array x .