code: def calculate_bins_nd(array, bins=None, *args, **kwargs):
      
      if kwargs.pop("check_nan", True):
          if np.any(np.isnan(array)):
              raise RuntimeError("Cannot calculate bins in presence of NaN's.")
  
      if array is not None:
          _, dim = array.shape
  
      # Prepare bins
      if isinstance(bins, (list, tuple)):
          if len(bins) != dim:
              raise RuntimeError("List of bins not understood, expected {0} items, got {1}."
                                 .format(dim, len(bins)))
      else:
          bins = [bins] * dim
  
      # Prepare arguments
      args = list(args)
      range_ = kwargs.pop("range", None)
      if range_:
          if len(range_) == 2 and all(np.isscalar(i) for i in range_):
              range_ = dim * [range_]
          elif len(range_) != dim:
              raise RuntimeError("Wrong dimensionality of range")
      for i in range(len(args)):
          if isinstance(args[i], (list, tuple)):
              if len(args[i]) != dim:
                  raise RuntimeError("Argument not understood.")
          else:
              args[i] = dim * [args[i]]
      for key in list(kwargs.keys()):
          if isinstance(kwargs[key], (list, tuple)):
              if len(kwargs[key]) != dim:
                  raise RuntimeError("Argument not understood.")
          else:
              kwargs[key] = dim * [kwargs[key]]
  
      if range_:
          kwargs["range"] = range_
  
      bins = [
          calculate_bins(array[:, i], bins[i],
                         *(arg[i] for arg in args if arg[i] is not None),
                         **{k: kwarg[i] for k, kwarg in kwargs.items() if kwarg[i] is not None})
          for i in range(dim)
          ]
      return bins
docs: Find optimal binning from arguments (n-dimensional variant)
  
      Usage similar to `calculate_bins`.
  
      Returns
      -------
      List[BinningBase]