code: def nargsort(
      items, kind="quicksort", ascending=True, na_position="last"
  ):
      """
      This is intended to be a drop-in replacement for np.argsort which
      handles NaNs. It adds ascending and na_position parameters.
      GH #6399, #5231
      """
  
      # specially handle Categorical
      if is_categorical_dtype(items):
          if na_position not in {"first", "last"}:
              raise ValueError(
                  "invalid na_position: {!r}".format(na_position)
              )
  
          mask = isna(items)
          cnt_null = mask.sum()
          sorted_idx = items.argsort(ascending=ascending, kind=kind)
          if ascending and na_position == "last":
              # NaN is coded as -1 and is listed in front after sorting
              sorted_idx = np.roll(sorted_idx, -cnt_null)
          elif not ascending and na_position == "first":
              # NaN is coded as -1 and is listed in the end after sorting
              sorted_idx = np.roll(sorted_idx, cnt_null)
          return sorted_idx
  
      with warnings.catch_warnings():
          # https://github.com/pandas-dev/pandas/issues/25439
          # can be removed once ExtensionArrays are properly handled by nargsort
          warnings.filterwarnings(
              "ignore",
              category=FutureWarning,
              message="Converting timezone-aware DatetimeArray to",
          )
          items = np.asanyarray(items)
      idx = np.arange(len(items))
      mask = isna(items)
      non_nans = items[~mask]
      non_nan_idx = idx[~mask]
      nan_idx = np.nonzero(mask)[0]
      if not ascending:
          non_nans = non_nans[::-1]
          non_nan_idx = non_nan_idx[::-1]
      indexer = non_nan_idx[non_nans.argsort(kind=kind)]
      if not ascending:
          indexer = indexer[::-1]
      # Finally, place the NaNs at the end or the beginning according to
      # na_position
      if na_position == "last":
          indexer = np.concatenate([indexer, nan_idx])
      elif na_position == "first":
          indexer = np.concatenate([nan_idx, indexer])
      else:
          raise ValueError(
              "invalid na_position: {!r}".format(na_position)
          )
      return indexer
  
imports: import warnings
  import numpy
  from pandas._libs import algos, hashtable, lib
  from pandas._libs.hashtable import unique_label_indices
  from pandas.compat import PY3, long, string_types
  from pandas.core.dtypes.cast import infer_dtype_from_array
  from pandas.core.dtypes.common import ensure_int64, ensure_platform_int, is_categorical_dtype, is_list_like
  from pandas.core.dtypes.missing import isna
  import pandas.core.algorithms
label_desc: Use image node_id 8 for calling a global function with example usage: nargsort(items, kind, ascending, na_position) and returns: indexer, sorted_idx