code: def _get_stat_data(
      data, x, y, group_by, method, deg, span, seed, max_n
  ):
      def _get_group_stat_data(group_df):
          xs, ys = _extract_data_series(group_df, x, y)
          if len(xs) == 0:
              return group_df.assign(**{_RESIDUAL_COL: []}), xs, ys
          predictor = _get_predictor(
              xs, ys, method, deg, span, seed, max_n
          )
          return (
              group_df.assign(**{_RESIDUAL_COL: ys - predictor(xs)}),
              xs,
              ys,
          )
  
      if isinstance(data, dict):
          df = pd.DataFrame(data)
      elif isinstance(data, pd.DataFrame):
          df = data.copy()
      elif pl is not None and isinstance(data, pl.DataFrame):
          df = pd.DataFrame(data.to_dict(False))
      else:
          raise Exception("Unsupported type of data: {0}".format(data))
      df = df[(df[x].notna()) & df[y].notna()]
      if group_by is None:
          return _get_group_stat_data(df)
      else:
          df_list, xs_list, ys_list = zip(
              *[
                  _get_group_stat_data(df[df[group_by] == group_value])
                  for group_value in df[group_by].unique()
              ]
          )
          return (
              pd.concat(df_list),
              np.concatenate(xs_list),
              np.concatenate(ys_list),
          )
  
file_path: lets-plot/python-package/lets_plot/bistro/residual.py
imports: from ..plot.core import PlotSpec, aes
  from ..plot.geom import geom_hline
  from ..plot.label import ylab
  from ..plot.theme_ import *
  from ._plot2d_common import _get_bin_params_2d, _get_geom2d_layer, _get_marginal_layers
label_desc: Use image node_id 6 for calling a global function with example usage: _get_stat_data(data, x, y, group_by, method, deg, span, seed, max_n) and returns: group_df,  xs,  ys, _get_group_stat_data, pd,  np,  np, group_df,  xs,  ys