Skip to content

Commit 4ac6eb1

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

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

pandas/core/generic.py

+6
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

+12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import json
66
import os
77

8+
import boto3
9+
from moto import mock_s3
810
import numpy as np
911
import pytest
1012

@@ -1662,3 +1664,13 @@ def test_json_multiindex(self, dataframe, expected):
16621664
series = dataframe.stack()
16631665
result = series.to_json(orient="index")
16641666
assert result == expected
1667+
1668+
@mock_s3
1669+
def test_to_s3(self):
1670+
# GH 28375
1671+
mock_bucket_name, target_file = "mybucket", "test.json"
1672+
conn = boto3.resource("s3")
1673+
bucket = conn.create_bucket(Bucket=mock_bucket_name)
1674+
df = DataFrame({"x": [1, 2, 3], "y": [2, 4, 6]})
1675+
df.to_json(f"s3://{mock_bucket_name}/{target_file}")
1676+
assert target_file in (obj.key for obj in bucket.objects.all())

0 commit comments

Comments
 (0)