Skip to content

Commit bd9a702

Browse files
authored
REGR: compressed to_json with URL-like paths and binary objects (#40010)
1 parent 85182df commit bd9a702

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

doc/source/whatsnew/v1.2.3.rst

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ 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`)
22+
-
2123

2224
.. ---------------------------------------------------------------------------
2325

pandas/io/json/_json.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def to_json(
130130
if path_or_buf is not None:
131131
# apply compression and byte/text conversion
132132
with get_handle(
133-
path_or_buf, "wt", compression=compression, storage_options=storage_options
133+
path_or_buf, "w", compression=compression, storage_options=storage_options
134134
) as handles:
135135
handles.handle.write(s)
136136
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
@@ -117,3 +119,13 @@ def test_to_json_compression(compression_only, read_infer, to_infer):
117119
df.to_json(path, compression=to_compression)
118120
result = pd.read_json(path, compression=read_compression)
119121
tm.assert_frame_equal(result, df)
122+
123+
124+
def test_to_json_compression_mode(compression):
125+
# GH 39985 (read_json does not support user-provided binary files)
126+
expected = pd.DataFrame({"A": [1]})
127+
128+
with BytesIO() as buffer:
129+
expected.to_json(buffer, compression=compression)
130+
# df = pd.read_json(buffer, compression=compression)
131+
# tm.assert_frame_equal(expected, df)

pandas/tests/io/test_fsspec.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,19 @@ def test_pickle_options(fsspectest):
248248

249249

250250
@td.skip_array_manager_not_yet_implemented # TODO(ArrayManager) JSON
251-
def test_json_options(fsspectest):
251+
def test_json_options(fsspectest, compression):
252252
df = DataFrame({"a": [0]})
253-
df.to_json("testmem://afile", storage_options={"test": "json_write"})
253+
df.to_json(
254+
"testmem://afile",
255+
compression=compression,
256+
storage_options={"test": "json_write"},
257+
)
254258
assert fsspectest.test[0] == "json_write"
255-
out = read_json("testmem://afile", storage_options={"test": "json_read"})
259+
out = read_json(
260+
"testmem://afile",
261+
compression=compression,
262+
storage_options={"test": "json_read"},
263+
)
256264
assert fsspectest.test[0] == "json_read"
257265
tm.assert_frame_equal(df, out)
258266

0 commit comments

Comments
 (0)