Skip to content

Commit 415f84d

Browse files
saitcakmakfacebook-github-bot
authored andcommitted
Fix deprecation warning from pandas.read_json (facebook#2346)
Summary: Pull Request resolved: facebook#2346 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 Reviewed By: mgarrard Differential Revision: D55978442 fbshipit-source-id: 078937c6a0fb0b216c85c299887f9c3352e49f4a
1 parent e143fd2 commit 415f84d

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

ax/core/data.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from abc import abstractmethod
1313
from functools import reduce
1414
from hashlib import md5
15+
from io import StringIO
1516
from typing import Any, Dict, Iterable, List, Optional, Set, Type, TypeVar, Union
1617

1718
import numpy as np
@@ -202,7 +203,7 @@ def deserialize_init_args(
202203
if "df" in args and not isinstance(args["df"], pd.DataFrame):
203204
# NOTE: Need dtype=False, otherwise infers arm_names like
204205
# "4_1" should be int 41.
205-
args["df"] = pd.read_json(args["df"]["value"], dtype=False)
206+
args["df"] = pd.read_json(StringIO(args["df"]["value"]), dtype=False)
206207
return extract_init_args(args=args, class_=cls)
207208

208209
@property

ax/core/tests/test_batch_trial.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,9 @@ def test_clone_to(self, _) -> None:
449449
batch.mark_running(no_runner_required=True)
450450
new_batch_trial = batch.clone_to()
451451
self.assertEqual(new_batch_trial.index, 2)
452-
# Set index to original trial's value for equality check.
452+
# Set index & time_created to original trial's value for equality check.
453453
new_batch_trial._index = batch.index
454+
new_batch_trial._time_created = batch._time_created
454455
self.assertEqual(new_batch_trial, batch)
455456

456457
def test_Runner(self) -> None:

ax/storage/json_store/decoder.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from collections import OrderedDict
1111
from enum import Enum
1212
from inspect import isclass
13+
from io import StringIO
1314
from logging import Logger
1415
from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union
1516

@@ -132,7 +133,7 @@ def object_from_json(
132133
elif _type == "DataFrame":
133134
# Need dtype=False, otherwise infers arm_names like "4_1"
134135
# should be int 41
135-
return pd.read_json(object_json["value"], dtype=False)
136+
return pd.read_json(StringIO(object_json["value"]), dtype=False)
136137
elif _type == "ndarray":
137138
return np.array(object_json["value"])
138139
elif _type == "Tensor":

ax/storage/sqa_store/decoder.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import json
1010
from collections import defaultdict, OrderedDict
1111
from enum import Enum
12+
from io import StringIO
1213
from logging import Logger
1314
from typing import Any, cast, Dict, List, Optional, Tuple, Type, Union
1415

@@ -982,7 +983,7 @@ def data_from_sqa(
982983
# Override df from deserialize_init_args with `data_json`.
983984
# NOTE: Need dtype=False, otherwise infers arm_names like
984985
# "4_1" should be int 41.
985-
kwargs["df"] = pd.read_json(data_sqa.data_json, dtype=False)
986+
kwargs["df"] = pd.read_json(StringIO(data_sqa.data_json), dtype=False)
986987

987988
dat = data_constructor(**kwargs)
988989

0 commit comments

Comments
 (0)