Skip to content
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

Fix Pandas codec decoding from numpy arrays #1751

Merged
merged 3 commits into from
May 16, 2024
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
3 changes: 1 addition & 2 deletions mlserver/codecs/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ def _to_series(input_or_output: InputOrOutput) -> pd.Series:
return pd.Series(payload)

if isinstance(payload, np.ndarray):
# Necessary so that it's compatible with pd.Series
payload = list(payload)
payload = payload.reshape(input_or_output.shape[0], 1).squeeze(axis=1)

dtype = to_dtype(input_or_output)
return pd.Series(payload, dtype=dtype)
Expand Down
2 changes: 1 addition & 1 deletion runtimes/mlflow/mlserver_mlflow/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _get_shape(input_spec: InputSpec) -> List[int]:
if isinstance(input_spec, TensorSpec):
return list(input_spec.shape)

return [-1]
return [-1, 1]


def to_metadata_tensors(
Expand Down
18 changes: 9 additions & 9 deletions runtimes/mlflow/tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_get_content_type(input_spec: InputSpec, expected: Tuple[MDatatype, str]
),
(
ColSpec(name="foo", type=DataType.string),
[-1],
[-1, 1],
),
],
)
Expand Down Expand Up @@ -131,13 +131,13 @@ def test_get_shape(input_spec: InputSpec, expected: List[int]):
MetadataTensor(
name="input-0",
datatype="BYTES",
shape=[-1],
shape=[-1, 1],
parameters=Parameters(content_type=StringCodec.ContentType),
),
MetadataTensor(
name="input-1",
datatype="INT32",
shape=[-1],
shape=[-1, 1],
parameters=Parameters(content_type=NumpyCodec.ContentType),
),
],
Expand Down Expand Up @@ -169,8 +169,8 @@ def test_to_metadata_tensors(schema: Schema, expected: List[MetadataTensor]):
name="foo",
datatype="BYTES",
parameters=Parameters(content_type=StringCodec.ContentType),
shape=[2, 11],
data=b"hello worldhello world",
shape=[2],
data=[b"hello", b"world"],
),
),
(
Expand All @@ -179,8 +179,8 @@ def test_to_metadata_tensors(schema: Schema, expected: List[MetadataTensor]):
name="foo",
datatype="BYTES",
parameters=Parameters(content_type=Base64Codec.ContentType),
shape=[1, 20],
data=b"UHl0aG9uIGlzIGZ1bg==",
shape=[1],
data=[b"UHl0aG9uIGlzIGZ1bg=="],
),
),
(
Expand All @@ -189,8 +189,8 @@ def test_to_metadata_tensors(schema: Schema, expected: List[MetadataTensor]):
name="foo",
datatype="BYTES",
parameters=Parameters(content_type=DatetimeCodec.ContentType),
shape=[1, 19],
data=b"2021-08-24T15:01:19",
shape=[1],
data=[b"2021-08-24T15:01:19"],
),
),
],
Expand Down
14 changes: 7 additions & 7 deletions tests/codecs/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,10 @@ def test_encode_request(
inputs=[
RequestInput(
name="a",
data=[1, 2, 3],
datatype="FP32",
shape=[1, 3],
parameters=Parameters(_decoded_payload=np.array([[1, 2, 3]])),
data=[[1, 2, 3]],
datatype="BYTES",
shape=[1, 1],
parameters=Parameters(_decoded_payload=[[1, 2, 3]]),
),
RequestInput(
name="b",
Expand All @@ -335,15 +335,15 @@ def test_encode_request(
),
]
),
pd.DataFrame({"a": [np.array([1, 2, 3])], "b": ["hello world"]}),
pd.DataFrame({"a": [[1, 2, 3]], "b": ["hello world"]}),
),
(
InferenceRequest(
inputs=[
RequestInput(
name="a",
data=[1, 2, 3],
datatype="FP32",
datatype="INT64",
shape=[3, 1],
parameters=Parameters(
_decoded_payload=np.array([[1], [2], [3]])
Expand All @@ -359,7 +359,7 @@ def test_encode_request(
),
pd.DataFrame(
{
"a": [[1], [2], [3]],
"a": [1, 2, 3],
"b": [a for a in b"ABC"],
}
),
Expand Down