Skip to content

BUG: to_json not allowing uploads to S3 (#28375) #31552

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 4 commits into from
Feb 2, 2020
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ MultiIndex
I/O
^^^
- Bug in :meth:`read_json` where integer overflow was occuring when json contains big number strings. (:issue:`30320`)
-
- Bug in :meth:`DataFrame.to_json` was raising ``NotFoundError`` when ``path_or_buf`` was an S3 URI (:issue:`28375`)
-

Plotting
Expand Down
13 changes: 6 additions & 7 deletions pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@
from pandas.core.construction import create_series_with_explicit_dtype
from pandas.core.reshape.concat import concat

from pandas.io.common import (
get_filepath_or_buffer,
get_handle,
infer_compression,
stringify_path,
)
from pandas.io.common import get_filepath_or_buffer, get_handle, infer_compression
from pandas.io.json._normalize import convert_to_line_delimits
from pandas.io.json._table_schema import build_table_schema, parse_table_schema
from pandas.io.parsers import _validate_integer
Expand Down Expand Up @@ -56,7 +51,11 @@ def to_json(
"'index=False' is only valid when 'orient' is 'split' or 'table'"
)

path_or_buf = stringify_path(path_or_buf)
if path_or_buf is not None:
path_or_buf, _, _, _ = get_filepath_or_buffer(
path_or_buf, compression=compression, mode="w"
)

if lines and orient != "records":
raise ValueError("'lines' keyword only valid when 'orient' is records")

Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1662,3 +1662,12 @@ def test_json_multiindex(self, dataframe, expected):
series = dataframe.stack()
result = series.to_json(orient="index")
assert result == expected

def test_to_s3(self, s3_resource):
# GH 28375
mock_bucket_name, target_file = "pandas-test", "test.json"
df = DataFrame({"x": [1, 2, 3], "y": [2, 4, 6]})
df.to_json(f"s3://{mock_bucket_name}/{target_file}")
assert target_file in (
obj.key for obj in s3_resource.Bucket("pandas-test").objects.all()
)