code: def builds_to_delete(platform: str, to_delete: Set[str]) -> int:
      try:
          pkgs_json = run_command(
              [
                  "conda",
                  "search",
                  "--json",
                  "-c",
                  "arrow-nightlies",
                  "--override-channels",
                  "--subdir",
                  platform,
              ],
          )
      except CommandFailedException as ex:
          # If the command failed due to no packages found, return
          # 0 builds to delete.
          if "PackagesNotFoundError" in ex.output:
              return 0
          else:
              sys.exit(1)
  
      pkgs = json.loads(pkgs_json)
      num_builds = 0
  
      for package_name, builds in pkgs.items():
          num_builds += len(builds)
          builds = pd.DataFrame(builds)
          builds["version"] = builds["version"].map(Version)
          # May be NaN if package doesn't depend on Python
          builds["py_version"] = builds["build"].str.extract(r"(py\d+)")
          builds["timestamp"] = pd.to_datetime(
              builds["timestamp"], unit="ms"
          )
          builds["stale"] = builds["timestamp"] < DELETE_BEFORE
          # Some packages can be present in several "features" (e.g. CUDA),
          # others miss that column in which case we set a default value.
          if "track_features" not in builds.columns:
              if package_name == "arrow-cpp-proc":
                  # XXX arrow-cpp-proc puts the features in the build field...
                  builds["track_features"] = builds["build"]
              else:
                  builds["track_features"] = 0
  
          # Detect old builds for each configuration:
          # a product of (architecture, Python version, features).
          for (
              subdir,
              python,
              features,
              stale,
          ), group in builds.groupby(
              ["subdir", "py_version", "track_features", "stale"],
              dropna=False,
          ):
              del_candidates = []
              if stale:
                  del_candidates = group
              else:
                  group = group.sort_values(
                      by="version", ascending=False
                  )
                  if len(group) > VERSIONS_TO_KEEP:
                      del_candidates = group[VERSIONS_TO_KEEP:]
  
              if len(del_candidates):
                  to_delete.update(
                      f"arrow-nightlies/{package_name}/"
                      + del_candidates["version"].astype(str)
                      + del_candidates["url"].str.replace(
                          "https://conda.anaconda.org/arrow-nightlies",
                          "",
                          regex=False,
                      )
                  )
  
      return num_builds
  
file_path: arrow/dev/tasks/conda-recipes/clean.py
imports: import subprocess
  from typing import Set
  import json
  import pandas
  import sys
  from packaging.version import Version
label_desc: Use image node_id 2 for calling a global function with example usage: builds_to_delete(platform, to_delete) and returns: num_builds, int