Skip to content

fix: wrap read_json args in StringIO for pandas 3.x compat - #477

Open
SudipSinha wants to merge 1 commit into
interpretml:mainfrom
SudipSinha:fix/pandas3-read-json-stringio
Open

fix: wrap read_json args in StringIO for pandas 3.x compat#477
SudipSinha wants to merge 1 commit into
interpretml:mainfrom
SudipSinha:fix/pandas3-read-json-stringio

Conversation

@SudipSinha

Copy link
Copy Markdown

Summary

pandas 3.x removed support for passing a raw JSON string directly to pd.read_json. It now treats all strings as file paths, raising FileNotFoundError when the string is inline JSON:

FileNotFoundError: File {"Categorical":{"0":"b"},"Numerical":{"0":25},...} does not exist

Fix: wrap the JSON string in io.StringIO to pass a file-like object instead. Works on pandas 2.x and 3.x.

File: dice_ml/diverse_counterfactuals.pyCounterfactualExamples.from_json (2 call sites at lines 226 and 229)

# before
test_instance_df = pd.read_json(cf_example_dict[...])
cfs_df = pd.read_json(cf_example_dict[...])

# after
from io import StringIO
test_instance_df = pd.read_json(StringIO(cf_example_dict[...]))
cfs_df = pd.read_json(StringIO(cf_example_dict[...]))

Impact

Fixes 13 failing tests across TestSerializationCounterfactualExplanations, TestDiceKDRegressionMethods, and TestDiceRandomRegressionMethods. No change in parsed output — StringIO only changes the I/O path, not the parsing.

Test plan

  • All 6 TestSerializationCounterfactualExplanations tests now pass
  • 3 TestDiceKDRegressionMethods deserialization tests now pass
  • 4 TestDiceRandomRegressionMethods::test_random_output (sklearn backend) now pass
  • Full suite: 266 passed, 14 failed (down from 253 passed, 27 failed) — remaining 14 are a separate numpy 2.x issue fixed in fix: squeeze regressor scores before scalar assignment for numpy 2.x compat #476

Tested on Python 3.12, 3.13, 3.14 with pandas 3.0.5.

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 <sudip.sinha@redhat.com>
Signed-off-by: Sudip Sinha <Sudip.Sinha@RedHat.com>

rh-pre-commit.version: 2.3.2
rh-pre-commit.check-secrets: ENABLED
@SudipSinha

Copy link
Copy Markdown
Author

Fixes #422 (pandas 3.x support).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant