From d4e9325d97ded12125a129c697f0e4a32cb50d51 Mon Sep 17 00:00:00 2001 From: Sudip Sinha Date: Wed, 9 Sep 2026 17:56:31 +0100 Subject: [PATCH] fix: wrap read_json args in StringIO for pandas 3.x compat MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pandas 3.x no longer accepts a raw JSON string as the path_or_buf argument to pd.read_json — it treats all strings as file paths, raising FileNotFoundError when the string is inline JSON. Wrapping with io.StringIO passes a file-like object instead, which works across pandas 2.x and 3.x. Fixes 13 failing tests in TestSerializationCounterfactualExplanations, TestDiceKDRegressionMethods, and TestDiceRandomRegressionMethods. Signed-off-by: Sudip Sinha Signed-off-by: Sudip Sinha rh-pre-commit.version: 2.3.2 rh-pre-commit.check-secrets: ENABLED --- dice_ml/diverse_counterfactuals.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/dice_ml/diverse_counterfactuals.py b/dice_ml/diverse_counterfactuals.py index 23027ea0..d7f20f17 100644 --- a/dice_ml/diverse_counterfactuals.py +++ b/dice_ml/diverse_counterfactuals.py @@ -1,6 +1,7 @@ import copy import json import math +from io import StringIO import numpy as np import pandas as pd @@ -223,10 +224,10 @@ def to_json(self, serialization_version): def from_json(cf_example_json_str): cf_example_dict = json.loads(cf_example_json_str) if cf_example_dict.get(_DiverseCFV1SchemaConstants.TEST_INSTANCE_DF) is not None: - test_instance_df = pd.read_json(cf_example_dict[ - _DiverseCFV1SchemaConstants.TEST_INSTANCE_DF]) + test_instance_df = pd.read_json(StringIO(cf_example_dict[ + _DiverseCFV1SchemaConstants.TEST_INSTANCE_DF])) if cf_example_dict[_DiverseCFV1SchemaConstants.FINAL_CFS_DF] is not None: - cfs_df = pd.read_json(cf_example_dict[_DiverseCFV1SchemaConstants.FINAL_CFS_DF]) + cfs_df = pd.read_json(StringIO(cf_example_dict[_DiverseCFV1SchemaConstants.FINAL_CFS_DF])) else: cfs_df = None