diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index e07a8fa0469f4..3304658a37a1a 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -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 diff --git a/pandas/io/json/_json.py b/pandas/io/json/_json.py index 204807b55c877..04fd17a00041b 100644 --- a/pandas/io/json/_json.py +++ b/pandas/io/json/_json.py @@ -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 @@ -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") diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py index 94d51589023c4..602022a21c4a6 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -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() + )