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 1 commit
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: 0 additions & 1 deletion doc/source/whatsnew/v1.0.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ I/O

- Fixed regression in :meth:`~DataFrame.to_csv` where specifying an ``na_rep`` might truncate the values written (:issue:`31447`)
-
-

Plotting
^^^^^^^^
Expand Down
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
2 changes: 2 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
from pandas.core.missing import find_valid_index
from pandas.core.ops import _align_method_FRAME

from pandas.io.common import get_filepath_or_buffer
from pandas.io.formats import format as fmt
from pandas.io.formats.format import DataFrameFormatter, format_percentiles
from pandas.io.formats.printing import pprint_thing
Expand Down Expand Up @@ -2259,6 +2260,7 @@ def to_json(
config.is_nonnegative_int(indent)
indent = indent or 0


return json.to_json(
path_or_buf=path_or_buf,
obj=self,
Expand Down
6 changes: 5 additions & 1 deletion pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,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()
)