Skip to content

feature: Add pandas deserializer #1738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/sagemaker/deserializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@

import numpy as np

from sagemaker.utils import DeferredError

try:
import pandas
except ImportError as e:
pandas = DeferredError(e)


class BaseDeserializer(abc.ABC):
"""Abstract base class for creation of new deserializers.
Expand Down Expand Up @@ -208,3 +215,31 @@ def deserialize(self, stream, content_type):
return json.load(codecs.getreader("utf-8")(stream))
finally:
stream.close()


class PandasDeserializer(BaseDeserializer):
"""Deserialize CSV or JSON data from an inference endpoint into a pandas dataframe."""

ACCEPT = "text/csv"

def deserialize(self, stream, content_type):
"""Deserialize CSV or JSON data from an inference endpoint into a pandas
dataframe.

If the data is JSON, the data should be formatted in the 'columns' orient.
See https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_json.html

Args:
stream (botocore.response.StreamingBody): Data to be deserialized.
content_type (str): The MIME type of the data.

Returns:
pandas.DataFrame: The data deserialized into a pandas DataFrame.
"""
if content_type == "text/csv":
return pandas.read_csv(stream)

if content_type == "application/json":
return pandas.read_json(stream)

raise ValueError("%s cannot read content type %s." % (__class__.__name__, content_type))
25 changes: 25 additions & 0 deletions tests/unit/sagemaker/test_deserializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
from __future__ import absolute_import

import io
import json

import numpy as np
import pandas as pd
import pytest

from sagemaker.deserializers import (
Expand All @@ -24,6 +26,7 @@
StreamDeserializer,
NumpyDeserializer,
JSONDeserializer,
PandasDeserializer,
)


Expand Down Expand Up @@ -171,3 +174,25 @@ def test_json_deserializer_invalid_data(json_deserializer):
with pytest.raises(ValueError) as error:
json_deserializer.deserialize(io.BytesIO(b"[[1]"), "application/json")
assert "column" in str(error)


@pytest.fixture
def pandas_deserializer():
return PandasDeserializer()


def test_pandas_deserializer_json(pandas_deserializer):
data = {"col 1": {"row 1": "a", "row 2": "c"}, "col 2": {"row 1": "b", "row 2": "d"}}
stream = io.StringIO(json.dumps(data))
result = pandas_deserializer.deserialize(stream, "application/json")
expected = pd.DataFrame(
[["a", "b"], ["c", "d"]], index=["row 1", "row 2"], columns=["col 1", "col 2"]
)
assert result.equals(expected)


def test_pandas_deserializer_csv(pandas_deserializer):
stream = io.StringIO("col 1,col 2\na,b\nc,d")
result = pandas_deserializer.deserialize(stream, "text/csv")
expected = pd.DataFrame([["a", "b"], ["c", "d"]], columns=["col 1", "col 2"])
assert result.equals(expected)