From b6eab1a425a9af0ec8643c5be62031640c3ca9a4 Mon Sep 17 00:00:00 2001 From: Sait Cakmak Date: Wed, 10 Apr 2024 11:37:13 -0700 Subject: [PATCH] Fix deprecation warning from pandas.read_json Summary: Addresses the following warning: ``` Passing literal json to 'read_json' is deprecated and will be removed in a future version. To read from a literal string, wrap it in a 'StringIO' object. ``` Seen in dev API tutorial: https://ax.dev/tutorials/gpei_hartmann_developer.html Differential Revision: D55978442 --- ax/core/data.py | 3 ++- ax/storage/json_store/decoder.py | 3 ++- ax/storage/sqa_store/decoder.py | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/ax/core/data.py b/ax/core/data.py index d22481ff720..3d9f9e8c509 100644 --- a/ax/core/data.py +++ b/ax/core/data.py @@ -12,6 +12,7 @@ from abc import abstractmethod from functools import reduce from hashlib import md5 +from io import StringIO from typing import Any, Dict, Iterable, List, Optional, Set, Type, TypeVar, Union import numpy as np @@ -202,7 +203,7 @@ def deserialize_init_args( if "df" in args and not isinstance(args["df"], pd.DataFrame): # NOTE: Need dtype=False, otherwise infers arm_names like # "4_1" should be int 41. - args["df"] = pd.read_json(args["df"]["value"], dtype=False) + args["df"] = pd.read_json(StringIO(args["df"]["value"]), dtype=False) return extract_init_args(args=args, class_=cls) @property diff --git a/ax/storage/json_store/decoder.py b/ax/storage/json_store/decoder.py index 872c28589f4..c3a4826889d 100644 --- a/ax/storage/json_store/decoder.py +++ b/ax/storage/json_store/decoder.py @@ -10,6 +10,7 @@ from collections import OrderedDict from enum import Enum from inspect import isclass +from io import StringIO from logging import Logger from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union @@ -132,7 +133,7 @@ def object_from_json( elif _type == "DataFrame": # Need dtype=False, otherwise infers arm_names like "4_1" # should be int 41 - return pd.read_json(object_json["value"], dtype=False) + return pd.read_json(StringIO(object_json["value"]), dtype=False) elif _type == "ndarray": return np.array(object_json["value"]) elif _type == "Tensor": diff --git a/ax/storage/sqa_store/decoder.py b/ax/storage/sqa_store/decoder.py index caaa2dab008..66d703e274e 100644 --- a/ax/storage/sqa_store/decoder.py +++ b/ax/storage/sqa_store/decoder.py @@ -9,6 +9,7 @@ import json from collections import defaultdict, OrderedDict from enum import Enum +from io import StringIO from logging import Logger from typing import Any, cast, Dict, List, Optional, Tuple, Type, Union @@ -982,7 +983,7 @@ def data_from_sqa( # Override df from deserialize_init_args with `data_json`. # NOTE: Need dtype=False, otherwise infers arm_names like # "4_1" should be int 41. - kwargs["df"] = pd.read_json(data_sqa.data_json, dtype=False) + kwargs["df"] = pd.read_json(StringIO(data_sqa.data_json), dtype=False) dat = data_constructor(**kwargs)