code: def nsdiffs(x, m, max_D=2, test='ocsb', **kwargs):
      
      if max_D <= 0:
          raise ValueError('max_D must be a positive integer')
  
      # get the test - this validates m internally
      testfunc = get_callable(test, VALID_STESTS)(m, **kwargs)\
          .estimate_seasonal_differencing_term
      x = column_or_1d(check_array(x, ensure_2d=False,
                                   force_all_finite=True, dtype=DTYPE))
  
      if is_constant(x):
          return 0
  
      D = 0
      dodiff = testfunc(x)
      while dodiff == 1 and D < max_D:
          D += 1
          x = diff(x, lag=m)
  
          if is_constant(x):
              return D
          dodiff = testfunc(x)
  
      return D
docs: Estimate the seasonal differencing term, ``D``.
  
      Perform a test of seasonality for different levels of ``D`` to
      estimate the number of seasonal differences required to make a given time
      series stationary. Will select the maximum value of ``D`` for which
      the time series is judged seasonally stationary by the statistical test.
  
      Parameters
      ----------
      x : array-like, shape=(n_samples, [n_features])
          The array to difference.
  
      m : int
          The number of seasonal periods (i.e., frequency of the
          time series)
  
      max_D : int, optional (default=2)
          Maximum number of seasonal differences allowed. Must
          be a positive integer. The estimated value of ``D`` will not
          exceed ``max_D``.
  
      test : str, optional (default='ocsb')
          Type of unit root test of seasonality to use in order
          to detect seasonal periodicity. Valid tests include ("ocsb", "ch").
          Note that the CHTest is very slow for large data.
  
      Returns
      -------
      D : int
          The estimated seasonal differencing term. This is the maximum value
          of ``D`` such that ``D <= max_D`` and the time series is judged
          seasonally stationary. If the time series is constant, will return 0.