Skip to content

Commit de2d33c

Browse files
authored
Add format to numpy and pandas parameter types (#53)
* Add format to numpy and pandas parameter types Co-authored-by: Neehar Duvvuri <[email protected]>
1 parent 95199ee commit de2d33c

10 files changed

+18
-1
lines changed

inference_schema/parameter_types/_constants.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@
77
TIME_FORMAT = "%H:%M:%S.%f %z"
88
ERR_PYTHON_DATA_NOT_JSON_SERIALIZABLE = "Invalid python data sample provided: ensure that the data is fully JSON " \
99
"serializable to be able to generate swagger schema from it. Actual error: {}"
10+
11+
12+
class SWAGGER_FORMAT_CONSTANTS:
13+
NUMPY_FORMAT = "numpy.ndarray"
14+
PANDAS_FORMAT = "pandas.DataFrame:{}"

inference_schema/parameter_types/numpy_parameter_type.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import numpy as np
66
from .abstract_parameter_type import AbstractParameterType
77
from ._swagger_from_dtype import Dtype2Swagger
8+
from ._constants import SWAGGER_FORMAT_CONSTANTS
89

910

1011
class NumpyParameterType(AbstractParameterType):
@@ -96,6 +97,7 @@ def input_to_swagger(self):
9697
swagger_schema = Dtype2Swagger.handle_swagger_array(swagger_item_type, shape)
9798
items_count = len(self.sample_input)
9899
swagger_schema['example'] = self._get_swagger_sample(self.sample_input, items_count, swagger_schema['items'])
100+
swagger_schema["format"] = SWAGGER_FORMAT_CONSTANTS.NUMPY_FORMAT
99101
return swagger_schema
100102

101103
@classmethod

inference_schema/parameter_types/pandas_parameter_type.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pandas as pd
77
from .abstract_parameter_type import AbstractParameterType
88
from ._util import get_swagger_for_list, get_swagger_for_nested_dict
9+
from ._constants import SWAGGER_FORMAT_CONSTANTS
910

1011

1112
class PandasParameterType(AbstractParameterType):
@@ -143,5 +144,5 @@ def input_to_swagger(self):
143144
elif data_type.startswith('timedelta'):
144145
swagger_schema['properties']['data']['items']['properties'][str(column_name)]['format'] = \
145146
'timedelta'
146-
147+
swagger_schema["format"] = SWAGGER_FORMAT_CONSTANTS.PANDAS_FORMAT.format(self.orient)
147148
return swagger_schema

tests/resources/sample_nested_input_schema.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"properties": {
88
"input1": {
99
"type": "array",
10+
"format": "pandas.DataFrame:records",
1011
"items": {
1112
"type": "object",
1213
"required": ["name", "state"],
@@ -22,6 +23,7 @@
2223
},
2324
"input2": {
2425
"type": "array",
26+
"format": "numpy.ndarray",
2527
"items": {
2628
"type": "object",
2729
"properties": {

tests/resources/sample_nested_output_schema.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"properties": {
55
"output1": {
66
"type": "array",
7+
"format": "pandas.DataFrame:records",
78
"items": {
89
"type": "object",
910
"required": ["state"],
@@ -16,6 +17,7 @@
1617
},
1718
"output2": {
1819
"type": "array",
20+
"format": "numpy.ndarray",
1921
"items": {
2022
"type": "object",
2123
"properties": {

tests/resources/sample_numpy_input_schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"properties": {
44
"param": {
55
"type": "array",
6+
"format": "numpy.ndarray",
67
"items": {
78
"type": "object",
89
"properties": {

tests/resources/sample_numpy_output_schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"type": "array",
3+
"format": "numpy.ndarray",
34
"items": {
45
"type": "object",
56
"properties": {

tests/resources/sample_pandas_datetime_schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"properties": {
44
"param": {
55
"type": "array",
6+
"format": "pandas.DataFrame:records",
67
"items": {
78
"type": "object",
89
"required": [

tests/resources/sample_pandas_input_schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"properties": {
44
"param": {
55
"type": "array",
6+
"format": "pandas.DataFrame:records",
67
"items": {
78
"type": "object",
89
"properties": {

tests/resources/sample_pandas_output_schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"type": "array",
3+
"format": "pandas.DataFrame:records",
34
"items": {
45
"type": "object",
56
"properties": {

0 commit comments

Comments
 (0)