Skip to content

Commit b787862

Browse files
committed
BUG: to_json not allowing uploads to S3 (#28375)
1 parent fc813e7 commit b787862

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/source/whatsnew/v1.0.1.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ I/O
9191
^^^
9292

9393
- Fixed regression in :meth:`~DataFrame.to_csv` where specifying an ``na_rep`` might truncate the values written (:issue:`31447`)
94-
-
94+
- Bug in :meth:`DataFrame.to_json` was raising ``NotFoundError`` when ``path_or_buf`` was an S3 URI (:issue:`28375`)
9595
-
9696

9797
Plotting

pandas/core/generic.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
from pandas.core.missing import find_valid_index
9999
from pandas.core.ops import _align_method_FRAME
100100

101+
from pandas.io.common import get_filepath_or_buffer
101102
from pandas.io.formats import format as fmt
102103
from pandas.io.formats.format import DataFrameFormatter, format_percentiles
103104
from pandas.io.formats.printing import pprint_thing
@@ -2259,6 +2260,11 @@ def to_json(
22592260
config.is_nonnegative_int(indent)
22602261
indent = indent or 0
22612262

2263+
if path_or_buf is not None:
2264+
path_or_buf, _, _, _ = get_filepath_or_buffer(
2265+
path_or_buf, compression=compression, mode="w"
2266+
)
2267+
22622268
return json.to_json(
22632269
path_or_buf=path_or_buf,
22642270
obj=self,

pandas/tests/io/json/test_pandas.py

Lines changed: 9 additions & 0 deletions
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)