class_docstr: Compute 2D kernel density estimation
  
  {usage}
  
  Parameters
  ----------
  {common_parameters}
  contour : bool, default=True
      Whether to create contours of the 2d density estimate.
  n : int, default=64
      Number of equally spaced points at which the density is to
      be estimated. For efficient computation, it should be a power
      of two.
  levels : int | array_like, default=5
      Contour levels. If an integer, it specifies the maximum number
      of levels, if array_like it is the levels themselves.
  package : Literal["statsmodels", "scipy", "sklearn"], default="statsmodels"
      Package whose kernel density estimation to use.
  kde_params : dict
      Keyword arguments to pass on to the kde class.
  
  See Also
  --------
  statsmodels.nonparametric.kernel_density.KDEMultivariate
  scipy.stats.gaussian_kde
  sklearn.neighbors.KernelDensity
code: def compute_group(cls, data, scales, **params):
      package = params["package"]
      kde_params = params["kde_params"]
  
      group = data["group"].iloc[0]
      range_x = scales.x.dimension()
      range_y = scales.y.dimension()
      x = np.linspace(range_x[0], range_x[1], params["n"])
      y = np.linspace(range_y[0], range_y[1], params["n"])
  
      # The grid must have a "similar" shape (n, p) to the var_data
      X, Y = np.meshgrid(x, y)
      var_data = np.array(
          [data["x"].to_numpy(), data["y"].to_numpy()]
      ).T
      grid = np.array([X.flatten(), Y.flatten()]).T
      density = kde(var_data, grid, package, **kde_params)
  
      if params["contour"]:
          Z = density.reshape(len(x), len(y))
          data = contour_lines(X, Y, Z, params["levels"])
          # Each piece should have a distinct group
          groups = str(group) + "-00" + data["piece"].astype(str)
          data["group"] = groups
      else:
          data = pd.DataFrame(
              {
                  "x": X.flatten(),
                  "y": Y.flatten(),
                  "density": density.flatten(),
                  "group": group,
                  "level": 1,
                  "piece": 1,
              }
          )
  
      return data
  
file_path: plotnine/plotnine/stats/stat_density_2d.py
imports: import numpy
  import pandas
  from ..doctools import document
  from .density import get_var_type, kde
  from .stat import stat
label_desc: Use image node_id 2 for calling the stat_density_2d obj's underlying member method code with example usage: obj.compute_group(cls, data, scales) and returns: data