code: def fill_linear_then_rolling_avg(series, limit_linear, rolling):
      """Adds missing dates, fills missing values with linear imputation or trend.
  
      Parameters
      ----------
          series : pd.Series
              series with nan to be filled in.
          limit_linear : int
              maximum number of missing values to impute.
  
              Note
              ----
              because imputation is done in both directions, this value is effectively doubled.
  
          rolling : int
              maximal number of missing values to impute.
  
              Note
              ----
              window width is rolling + 2*limit_linear
  
      Returns
      -------
          pd.DataFrame
              manipulated dataframe containing filled values
      """
      # impute small gaps linearly:
      series = pd.to_numeric(series)
      series = series.interpolate(
          method="linear", limit=limit_linear, limit_direction="both"
      )
      # fill remaining gaps with rolling avg
      is_na = pd.isna(series)
      rolling_avg = series.rolling(
          rolling + 2 * limit_linear,
          min_periods=2 * limit_linear,
          center=True,
      ).mean()
      series.loc[is_na] = rolling_avg[is_na]
      remaining_na = sum(series.isnull())
      return series, remaining_na
  
imports: from __future__ import annotations
  import logging
  import math
  from collections import OrderedDict
  from dataclasses import dataclass
  from typing import TYPE_CHECKING, List, Optional, Tuple
  import numpy
  import pandas
label_desc: Use image node_id 24 for calling a global function with example usage: fill_linear_then_rolling_avg(series, limit_linear, rolling) and returns: series,  remaining_na