File tree Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -2400,6 +2400,7 @@ def to_json(
2400
2400
index : bool_t = True ,
2401
2401
indent : int | None = None ,
2402
2402
storage_options : StorageOptions = None ,
2403
+ mode : str = 'w' ,
2403
2404
) -> str | None :
2404
2405
"""
2405
2406
Convert the object to a JSON string.
@@ -2478,6 +2479,11 @@ def to_json(
2478
2479
2479
2480
.. versionadded:: 1.2.0
2480
2481
2482
+ mode : str, default 'w' (writing)
2483
+ Specify the IO mode for output when supplying a path_or_buf.
2484
+ Accepted args are 'w' (writing) and 'a' (append) only.
2485
+ Supplying mode='a' is only supported when lines is True and orient is 'records'.
2486
+
2481
2487
Returns
2482
2488
-------
2483
2489
None or str
@@ -2661,6 +2667,7 @@ def to_json(
2661
2667
index = index ,
2662
2668
indent = indent ,
2663
2669
storage_options = storage_options ,
2670
+ mode = mode ,
2664
2671
)
2665
2672
2666
2673
@final
Original file line number Diff line number Diff line change @@ -136,6 +136,7 @@ def to_json(
136
136
index : bool = True ,
137
137
indent : int = 0 ,
138
138
storage_options : StorageOptions = None ,
139
+ mode : str = 'w' ,
139
140
) -> str | None :
140
141
141
142
if not index and orient not in ["split" , "table" ]:
@@ -146,6 +147,17 @@ def to_json(
146
147
if lines and orient != "records" :
147
148
raise ValueError ("'lines' keyword only valid when 'orient' is records" )
148
149
150
+ if mode not in ['a' , 'w' ]:
151
+ raise ValueError (
152
+ f"mode={ mode !r} is not a valid option. Only 'w' and 'a' are currently supported."
153
+ )
154
+
155
+ if mode == 'a' and (not lines or orient != 'records' ):
156
+ raise ValueError (
157
+ "mode='a' (append) is only supported when lines is True and orient is 'records'"
158
+ )
159
+
160
+
149
161
if orient == "table" and isinstance (obj , Series ):
150
162
obj = obj .to_frame (name = obj .name or "values" )
151
163
@@ -177,7 +189,7 @@ def to_json(
177
189
if path_or_buf is not None :
178
190
# apply compression and byte/text conversion
179
191
with get_handle (
180
- path_or_buf , "w" , compression = compression , storage_options = storage_options
192
+ path_or_buf , mode , compression = compression , storage_options = storage_options
181
193
) as handles :
182
194
handles .handle .write (s )
183
195
else :
You can’t perform that action at this time.
0 commit comments