Skip to content

Commit e2ae57c

Browse files
authored
BUG: to_json not allowing uploads to S3 (#28375) (#31552)
1 parent 26329fc commit e2ae57c

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

doc/source/whatsnew/v1.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ MultiIndex
174174
I/O
175175
^^^
176176
- Bug in :meth:`read_json` where integer overflow was occuring when json contains big number strings. (:issue:`30320`)
177-
-
177+
- Bug in :meth:`DataFrame.to_json` was raising ``NotFoundError`` when ``path_or_buf`` was an S3 URI (:issue:`28375`)
178178
-
179179

180180
Plotting

pandas/io/json/_json.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@
1919
from pandas.core.construction import create_series_with_explicit_dtype
2020
from pandas.core.reshape.concat import concat
2121

22-
from pandas.io.common import (
23-
get_filepath_or_buffer,
24-
get_handle,
25-
infer_compression,
26-
stringify_path,
27-
)
22+
from pandas.io.common import get_filepath_or_buffer, get_handle, infer_compression
2823
from pandas.io.json._normalize import convert_to_line_delimits
2924
from pandas.io.json._table_schema import build_table_schema, parse_table_schema
3025
from pandas.io.parsers import _validate_integer
@@ -56,7 +51,11 @@ def to_json(
5651
"'index=False' is only valid when 'orient' is 'split' or 'table'"
5752
)
5853

59-
path_or_buf = stringify_path(path_or_buf)
54+
if path_or_buf is not None:
55+
path_or_buf, _, _, _ = get_filepath_or_buffer(
56+
path_or_buf, compression=compression, mode="w"
57+
)
58+
6059
if lines and orient != "records":
6160
raise ValueError("'lines' keyword only valid when 'orient' is records")
6261

pandas/tests/io/json/test_pandas.py

+9
Original file line numberDiff line numberDiff line change
@@ -1662,3 +1662,12 @@ def test_json_multiindex(self, dataframe, expected):
16621662
series = dataframe.stack()
16631663
result = series.to_json(orient="index")
16641664
assert result == expected
1665+
1666+
def test_to_s3(self, s3_resource):
1667+
# GH 28375
1668+
mock_bucket_name, target_file = "pandas-test", "test.json"
1669+
df = DataFrame({"x": [1, 2, 3], "y": [2, 4, 6]})
1670+
df.to_json(f"s3://{mock_bucket_name}/{target_file}")
1671+
assert target_file in (
1672+
obj.key for obj in s3_resource.Bucket("pandas-test").objects.all()
1673+
)

0 commit comments

Comments
 (0)