def array_to_csv()

in src/sagemaker_training/encoders.py [0:0]


def array_to_csv(array_like):
    """Convert an array like object to CSV.

    To understand what an array-like object is, please see:
    https://docs.scipy.org/doc/numpy/user/basics.creation.html#converting-python-array-like-objects-to-numpy-arrays

    Args:
        array_like (np.array or Iterable or int or float): Array-like object to be converted to CSV.

    Returns:
        (str): Object serialized to CSV.
    """
    array = np.array(array_like)
    if len(array.shape) == 1:
        array = np.reshape(array, (array.shape[0], 1))  # pylint: disable=unsubscriptable-object

    try:
        stream = StringIO()
        writer = csv.writer(
            stream, lineterminator="\n", delimiter=",", quotechar='"', doublequote=True, strict=True
        )
        writer.writerows(array)
        return stream.getvalue()
    except csv.Error as e:
        raise errors.ClientError("Error while encoding csv: {}".format(e))