Skip to content

Backport PR #40010 on branch 1.2.x #40040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Fixed regressions
- Fixed regression in :meth:`~DataFrame.to_excel` raising ``KeyError`` when giving duplicate columns with ``columns`` attribute (:issue:`39695`)
- Fixed regression in nullable integer unary ops propagating mask on assignment (:issue:`39943`)
- Fixed regression in :meth:`DataFrame.__setitem__` not aligning :class:`DataFrame` on right-hand side for boolean indexer (:issue:`39931`)
- 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`)

.. ---------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def to_json(
if path_or_buf is not None:
# apply compression and byte/text conversion
with get_handle(
path_or_buf, "wt", compression=compression, storage_options=storage_options
path_or_buf, "w", compression=compression, storage_options=storage_options
) as handles:
handles.handle.write(s)
else:
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/io/json/test_compression.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from io import BytesIO

import pytest

import pandas.util._test_decorators as td
Expand Down Expand Up @@ -115,3 +117,13 @@ def test_to_json_compression(compression_only, read_infer, to_infer):
df.to_json(path, compression=to_compression)
result = pd.read_json(path, compression=read_compression)
tm.assert_frame_equal(result, df)


def test_to_json_compression_mode(compression):
# GH 39985 (read_json does not support user-provided binary files)
expected = pd.DataFrame({"A": [1]})

with BytesIO() as buffer:
expected.to_json(buffer, compression=compression)
# df = pd.read_json(buffer, compression=compression)
# tm.assert_frame_equal(expected, df)
14 changes: 11 additions & 3 deletions pandas/tests/io/test_fsspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,19 @@ def test_pickle_options(fsspectest):
tm.assert_frame_equal(df, out)


def test_json_options(fsspectest):
def test_json_options(fsspectest, compression):
df = DataFrame({"a": [0]})
df.to_json("testmem://afile", storage_options={"test": "json_write"})
df.to_json(
"testmem://afile",
compression=compression,
storage_options={"test": "json_write"},
)
assert fsspectest.test[0] == "json_write"
out = read_json("testmem://afile", storage_options={"test": "json_read"})
out = read_json(
"testmem://afile",
compression=compression,
storage_options={"test": "json_read"},
)
assert fsspectest.test[0] == "json_read"
tm.assert_frame_equal(df, out)

Expand Down