def get_df_info(
  df: pd.DataFrame,
  name: Optional[str] = None,
  deep: bool = False,
  counter: bool = False,
) -> str:
  """Log dtype and RAM usage stats for each input DataFrame."""
  stats = (
    df.dtypes.to_frame().reset_index(drop=False).rename(columns={"index": "column", 0: "dtype"})
  ).merge(
    # deep=True shows memory usage for the entire contained object (e.g. if the type
    # of a column is "object", then deep=True shows the size of the objects instead
    # of the size of the pointers.
    df.memory_usage(index=True, deep=deep)
    .to_frame()
    .reset_index(drop=False)
    .rename(columns={"index": "column", 0: "RAM"})
  )
  ramBytes = stats["RAM"].sum()
  if name is not None:
    lines = [f"""{name} total RAM: {ramBytes} bytes ({ramBytes * 1e-9:.3f} GB)"""]
  else:
    lines = [f"""total RAM: {ramBytes} bytes ({ramBytes * 1e-9:.3f} GB)"""]
  lines.extend(str(stats).split("\n"))
  if counter:
    for col, dtype in zip(stats["column"], stats["dtype"]):
      if dtype != object:
        continue
      lines.append(f"{col}: {Counter(type(obj) for obj in df[col])}")
  return "\n".join(lines)
Log dtype and RAM usage stats for each input DataFrame.
sourcecode/scoring/pandas_utils.py