import random


def partitionByState(ser, holdouts=1):
  """
  Creates training and test indexes by randomly selecting
  a indices for each state.
  :param pd.DataFrame ser: Classes for instances
  :param int holdouts: number of holdouts for test
  :return list-object, list-object: test, train
  """
  classes = ser.unique().tolist()
  classes.sort()
  test_idxs = []
  for cls in classes:
    ser_cls = ser[ser == cls]
    if len(ser_cls) <= holdouts:
      raise ValueError(
          "Class %s has fewer than %d holdouts" %
          (cls, holdouts))
    idxs = random.sample(ser_cls.index.tolist(),
        holdouts)
    test_idxs.extend(idxs)
  #
  train_idxs = list(set(ser.index).difference(test_idxs))
  return train_idxs, test_idxs