Skip to content

Commit

Permalink
Fix deprecation warning from pandas.read_json (#2346)
Browse files Browse the repository at this point in the history
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

Reviewed By: mgarrard

Differential Revision: D55978442
  • Loading branch information
saitcakmak authored and facebook-github-bot committed Apr 10, 2024
1 parent 2265497 commit de2af43
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion ax/core/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion ax/core/tests/test_batch_trial.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,9 @@ def test_clone_to(self, _) -> None:
batch.mark_running(no_runner_required=True)
new_batch_trial = batch.clone_to()
self.assertEqual(new_batch_trial.index, 2)
# Set index to original trial's value for equality check.
# Set index & time_created to original trial's value for equality check.
new_batch_trial._index = batch.index
new_batch_trial._time_created = batch._time_created
self.assertEqual(new_batch_trial, batch)

def test_Runner(self) -> None:
Expand Down
3 changes: 2 additions & 1 deletion ax/storage/json_store/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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":
Expand Down
3 changes: 2 additions & 1 deletion ax/storage/sqa_store/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit de2af43

Please sign in to comment.