code: def ndiffs(x, alpha=0.05, test="kpss", kind="level", max_d=2):
      x = x[~np.isnan(x)]
      d = 0
      if alpha < 0.01:
          warnings.warn(
              "Specified alpha value is less than the minimum, setting alpha=0.01"
          )
          alpha = 0.01
      elif alpha > 0.1:
          warnings.warn(
              "Specified alpha value is larger than the maximum, setting alpha=0.1"
          )
          alpha = 0.1
      if is_constant(x):
          return d
  
      def run_tests(x, test, alpha):
          try:
              with warnings.catch_warnings():
                  warnings.simplefilter("ignore")
                  nlags = math.floor(3 * math.sqrt(len(x)) / 13)
                  diff = sm.tsa.kpss(x, "c", nlags=nlags)[1] < alpha
          except Exception as e:
              warnings.warn(
                  f"The chosen unit root test encountered an error when testing for the {d} difference.\n"
                  f"From {test}(): {e}\n"
                  f"{d} differences will be used. Consider using a different unit root test."
              )
              diff = False
          return diff
  
      dodiff = run_tests(x, test, alpha)
      if math.isnan(dodiff):
          return d
      while dodiff and d < max_d:
          d += 1
          x = diff(x, 1, 1)[1:]
          if is_constant(x):
              return d
          dodiff = run_tests(x, test, alpha)
          if math.isnan(dodiff):
              return d - 1
      return d
  
imports: import math
  import warnings
  from collections import namedtuple
  from functools import partial
  from typing import Optional, Dict, Union, Tuple
  import numpy
  import pandas
  import statsmodels.api
  from numba import njit
  from scipy.optimize import minimize
  from scipy.stats import norm
  from .mstl import mstl
  from .utils import CACHE, NOGIL
label_desc: Use image node_id 34 for calling a global function with example usage: ndiffs(x, alpha, test, kind, max_d) and returns: d, d, diff, d, d, unknown