def _num_columns(data):
  """Find the number of columns in a raw data source.

  Args:
    data: 2D numpy array, 1D record array, or list of lists representing the
      contents of the source dataframe.

  Returns:
    num_columns: number of columns in the data.
  """
  if hasattr(data, 'shape'):  # True for numpy arrays
    if data.ndim == 1:
      # 1D record array; number of columns is length of compound dtype.
      return len(data.dtype)
    elif data.ndim == 2:
      # 2D array of values: number of columns is in the shape.
      return data.shape[1]
    else:
      raise ValueError('data expected to be a 2D array or 1D record array.')
  elif data:
    # List of lists: first entry is first row.
    return len(data[0])
  else:
    # Empty list has zero columns
    return 0