Skip to content

Commit 4df87a3

Browse files
authored
Backport PR #40010: REGR: compressed to_json with URL-like paths and binary objects (#40040)
1 parent d29f019 commit 4df87a3

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

doc/source/whatsnew/v1.2.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Fixed regressions
1818
- Fixed regression in :meth:`~DataFrame.to_excel` raising ``KeyError`` when giving duplicate columns with ``columns`` attribute (:issue:`39695`)
1919
- Fixed regression in nullable integer unary ops propagating mask on assignment (:issue:`39943`)
2020
- Fixed regression in :meth:`DataFrame.__setitem__` not aligning :class:`DataFrame` on right-hand side for boolean indexer (:issue:`39931`)
21+
- Fixed regression in :meth:`~DataFrame.to_json` failing to use ``compression`` with URL-like paths that are internally opened in binary mode or with user-provided file objects that are opened in binary mode (:issue:`39985`)
2122

2223
.. ---------------------------------------------------------------------------
2324

pandas/io/json/_json.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def to_json(
100100
if path_or_buf is not None:
101101
# apply compression and byte/text conversion
102102
with get_handle(
103-
path_or_buf, "wt", compression=compression, storage_options=storage_options
103+
path_or_buf, "w", compression=compression, storage_options=storage_options
104104
) as handles:
105105
handles.handle.write(s)
106106
else:

pandas/tests/io/json/test_compression.py

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from io import BytesIO
2+
13
import pytest
24

35
import pandas.util._test_decorators as td
@@ -115,3 +117,13 @@ def test_to_json_compression(compression_only, read_infer, to_infer):
115117
df.to_json(path, compression=to_compression)
116118
result = pd.read_json(path, compression=read_compression)
117119
tm.assert_frame_equal(result, df)
120+
121+
122+
def test_to_json_compression_mode(compression):
123+
# GH 39985 (read_json does not support user-provided binary files)
124+
expected = pd.DataFrame({"A": [1]})
125+
126+
with BytesIO() as buffer:
127+
expected.to_json(buffer, compression=compression)
128+
# df = pd.read_json(buffer, compression=compression)
129+
# tm.assert_frame_equal(expected, df)

pandas/tests/io/test_fsspec.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,19 @@ def test_pickle_options(fsspectest):
249249
tm.assert_frame_equal(df, out)
250250

251251

252-
def test_json_options(fsspectest):
252+
def test_json_options(fsspectest, compression):
253253
df = DataFrame({"a": [0]})
254-
df.to_json("testmem://afile", storage_options={"test": "json_write"})
254+
df.to_json(
255+
"testmem://afile",
256+
compression=compression,
257+
storage_options={"test": "json_write"},
258+
)
255259
assert fsspectest.test[0] == "json_write"
256-
out = read_json("testmem://afile", storage_options={"test": "json_read"})
260+
out = read_json(
261+
"testmem://afile",
262+
compression=compression,
263+
storage_options={"test": "json_read"},
264+
)
257265
assert fsspectest.test[0] == "json_read"
258266
tm.assert_frame_equal(df, out)
259267

0 commit comments

Comments
 (0)