code: def clean_fill_method(
      method: str | None, allow_nearest: bool = False
  ):
      # asfreq is compat for resampling
      if method in [None, "asfreq"]:
          return None
  
      if isinstance(method, str):
          method = method.lower()
          if method == "ffill":
              method = "pad"
          elif method == "bfill":
              method = "backfill"
  
      valid_methods = ["pad", "backfill"]
      expecting = "pad (ffill) or backfill (bfill)"
      if allow_nearest:
          valid_methods.append("nearest")
          expecting = "pad (ffill), backfill (bfill) or nearest"
      if method not in valid_methods:
          raise ValueError(
              f"Invalid fill method. Expecting {expecting}. Got {method}"
          )
      return method
  
imports: from __future__ import annotations
  from functools import partial, wraps
  from typing import TYPE_CHECKING, Any, cast
  import numpy
  from pandas._libs import NaT, algos, lib
  from pandas._typing import ArrayLike, Axis, AxisInt, F, npt
  from pandas.compat._optional import import_optional_dependency
  from pandas.core.dtypes.cast import infer_dtype_from
  from pandas.core.dtypes.common import is_array_like, is_numeric_v_string_like, is_object_dtype, needs_i8_conversion
  from pandas.core.dtypes.missing import is_valid_na_for_dtype, isna, na_value_for_dtype
label_desc: Use image node_id 3 for calling a global function with example usage: clean_fill_method(method, allow_nearest) and returns: method, None