Skip to content

Commit dd2dc47

Browse files
committed
BUG: to_json not allowing uploads to S3 (pandas-dev#28375)
1 parent fc813e7 commit dd2dc47

File tree

5 files changed

+17
-3
lines changed

5 files changed

+17
-3
lines changed

doc/source/whatsnew/v1.0.1.rst

-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ I/O
9292

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

9796
Plotting
9897
^^^^^^^^

doc/source/whatsnew/v1.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ MultiIndex
167167
I/O
168168
^^^
169169
- Bug in :meth:`read_json` where integer overflow was occuring when json contains big number strings. (:issue:`30320`)
170-
-
170+
- Bug in :meth:`DataFrame.to_json` was raising ``NotFoundError`` when ``path_or_buf`` was an S3 URI (:issue:`28375`)
171171
-
172172

173173
Plotting

pandas/core/generic.py

+2
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,7 @@ def to_json(
22592260
config.is_nonnegative_int(indent)
22602261
indent = indent or 0
22612262

2263+
22622264
return json.to_json(
22632265
path_or_buf=path_or_buf,
22642266
obj=self,

pandas/io/json/_json.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ def to_json(
5656
"'index=False' is only valid when 'orient' is 'split' or 'table'"
5757
)
5858

59-
path_or_buf = stringify_path(path_or_buf)
59+
if path_or_buf is not None:
60+
path_or_buf, _, _, _ = get_filepath_or_buffer(
61+
path_or_buf, compression=compression, mode="w"
62+
)
63+
6064
if lines and orient != "records":
6165
raise ValueError("'lines' keyword only valid when 'orient' is records")
6266

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)