1
+ from collections import OrderedDict
1
2
from io import StringIO
2
3
from itertools import islice
3
4
import os
5
+ from typing import Any , Callable , Dict , List , Optional , Type , Union
4
6
5
7
import numpy as np
6
8
11
13
from pandas .core .dtypes .common import ensure_str , is_period_dtype
12
14
13
15
from pandas import DataFrame , MultiIndex , Series , isna , to_datetime
16
+ from pandas ._typing import Scalar
14
17
from pandas .core .reshape .concat import concat
15
18
16
19
from pandas .io .common import (
31
34
32
35
TABLE_SCHEMA_VERSION = "0.20.0"
33
36
37
+ Serializable = Union [Scalar , List , Dict ]
38
+
34
39
35
40
# interface to/from
36
41
def to_json (
37
42
path_or_buf ,
38
43
obj ,
39
- orient = None ,
40
- date_format = "epoch" ,
41
- double_precision = 10 ,
42
- force_ascii = True ,
43
- date_unit = "ms" ,
44
- default_handler = None ,
45
- lines = False ,
46
- compression = "infer" ,
47
- index = True ,
44
+ orient : Optional [str ] = None ,
45
+ date_format : str = "epoch" ,
46
+ double_precision : int = 10 ,
47
+ force_ascii : bool = True ,
48
+ date_unit : str = "ms" ,
49
+ default_handler : Optional [Callable [[Any ], Serializable ]] = None ,
50
+ lines : bool = False ,
51
+ compression : Optional [str ] = "infer" ,
52
+ index : bool = True ,
53
+ indent : int = 0 ,
48
54
):
49
55
50
56
if not index and orient not in ["split" , "table" ]:
@@ -59,7 +65,7 @@ def to_json(
59
65
if orient == "table" and isinstance (obj , Series ):
60
66
obj = obj .to_frame (name = obj .name or "values" )
61
67
if orient == "table" and isinstance (obj , DataFrame ):
62
- writer = JSONTableWriter
68
+ writer = JSONTableWriter # type: Type["Writer"]
63
69
elif isinstance (obj , Series ):
64
70
writer = SeriesWriter
65
71
elif isinstance (obj , DataFrame ):
@@ -76,6 +82,7 @@ def to_json(
76
82
date_unit = date_unit ,
77
83
default_handler = default_handler ,
78
84
index = index ,
85
+ indent = indent ,
79
86
).write ()
80
87
81
88
if lines :
@@ -97,18 +104,19 @@ class Writer:
97
104
def __init__ (
98
105
self ,
99
106
obj ,
100
- orient ,
101
- date_format ,
102
- double_precision ,
103
- ensure_ascii ,
104
- date_unit ,
105
- index ,
106
- default_handler = None ,
107
+ orient : Optional [str ],
108
+ date_format : str ,
109
+ double_precision : int ,
110
+ ensure_ascii : bool ,
111
+ date_unit : str ,
112
+ index : bool ,
113
+ default_handler : Optional [Callable [[Any ], Serializable ]] = None ,
114
+ indent : int = 0 ,
107
115
):
108
116
self .obj = obj
109
117
110
118
if orient is None :
111
- orient = self ._default_orient
119
+ orient = self ._default_orient # type: ignore
112
120
113
121
self .orient = orient
114
122
self .date_format = date_format
@@ -117,6 +125,7 @@ def __init__(
117
125
self .date_unit = date_unit
118
126
self .default_handler = default_handler
119
127
self .index = index
128
+ self .indent = indent
120
129
121
130
self .is_copy = None
122
131
self ._format_axes ()
@@ -133,17 +142,19 @@ def write(self):
133
142
self .date_unit ,
134
143
self .date_format == "iso" ,
135
144
self .default_handler ,
145
+ self .indent ,
136
146
)
137
147
138
148
def _write (
139
149
self ,
140
150
obj ,
141
- orient ,
142
- double_precision ,
143
- ensure_ascii ,
144
- date_unit ,
145
- iso_dates ,
146
- default_handler ,
151
+ orient : Optional [str ],
152
+ double_precision : int ,
153
+ ensure_ascii : bool ,
154
+ date_unit : str ,
155
+ iso_dates : bool ,
156
+ default_handler : Optional [Callable [[Any ], Serializable ]],
157
+ indent : int ,
147
158
):
148
159
return dumps (
149
160
obj ,
@@ -153,6 +164,7 @@ def _write(
153
164
date_unit = date_unit ,
154
165
iso_dates = iso_dates ,
155
166
default_handler = default_handler ,
167
+ indent = indent ,
156
168
)
157
169
158
170
@@ -169,12 +181,13 @@ def _format_axes(self):
169
181
def _write (
170
182
self ,
171
183
obj ,
172
- orient ,
173
- double_precision ,
174
- ensure_ascii ,
175
- date_unit ,
176
- iso_dates ,
177
- default_handler ,
184
+ orient : Optional [str ],
185
+ double_precision : int ,
186
+ ensure_ascii : bool ,
187
+ date_unit : str ,
188
+ iso_dates : bool ,
189
+ default_handler : Optional [Callable [[Any ], Serializable ]],
190
+ indent : int ,
178
191
):
179
192
if not self .index and orient == "split" :
180
193
obj = {"name" : obj .name , "data" : obj .values }
@@ -186,6 +199,7 @@ def _write(
186
199
date_unit ,
187
200
iso_dates ,
188
201
default_handler ,
202
+ indent ,
189
203
)
190
204
191
205
@@ -214,12 +228,13 @@ def _format_axes(self):
214
228
def _write (
215
229
self ,
216
230
obj ,
217
- orient ,
218
- double_precision ,
219
- ensure_ascii ,
220
- date_unit ,
221
- iso_dates ,
222
- default_handler ,
231
+ orient : Optional [str ],
232
+ double_precision : int ,
233
+ ensure_ascii : bool ,
234
+ date_unit : str ,
235
+ iso_dates : bool ,
236
+ default_handler : Optional [Callable [[Any ], Serializable ]],
237
+ indent : int ,
223
238
):
224
239
if not self .index and orient == "split" :
225
240
obj = obj .to_dict (orient = "split" )
@@ -232,6 +247,7 @@ def _write(
232
247
date_unit ,
233
248
iso_dates ,
234
249
default_handler ,
250
+ indent ,
235
251
)
236
252
237
253
@@ -241,20 +257,22 @@ class JSONTableWriter(FrameWriter):
241
257
def __init__ (
242
258
self ,
243
259
obj ,
244
- orient ,
245
- date_format ,
246
- double_precision ,
247
- ensure_ascii ,
248
- date_unit ,
249
- index ,
250
- default_handler = None ,
260
+ orient : Optional [str ],
261
+ date_format : str ,
262
+ double_precision : int ,
263
+ ensure_ascii : bool ,
264
+ date_unit : str ,
265
+ index : bool ,
266
+ default_handler : Optional [Callable [[Any ], Serializable ]] = None ,
267
+ indent : int = 0 ,
251
268
):
252
269
"""
253
270
Adds a `schema` attribute with the Table Schema, resets
254
271
the index (can't do in caller, because the schema inference needs
255
272
to know what the index is, forces orient to records, and forces
256
273
date_format to 'iso'.
257
274
"""
275
+
258
276
super ().__init__ (
259
277
obj ,
260
278
orient ,
@@ -264,6 +282,7 @@ def __init__(
264
282
date_unit ,
265
283
index ,
266
284
default_handler = default_handler ,
285
+ indent = indent ,
267
286
)
268
287
269
288
if date_format != "iso" :
@@ -315,19 +334,20 @@ def _write(
315
334
date_unit ,
316
335
iso_dates ,
317
336
default_handler ,
337
+ indent ,
318
338
):
319
- data = super ()._write (
320
- obj ,
339
+ table_obj = OrderedDict ((("schema" , self .schema ), ("data" , obj )))
340
+ serialized = super ()._write (
341
+ table_obj ,
321
342
orient ,
322
343
double_precision ,
323
344
ensure_ascii ,
324
345
date_unit ,
325
346
iso_dates ,
326
347
default_handler ,
348
+ indent ,
327
349
)
328
- serialized = '{{"schema": {schema}, "data": {data}}}' .format (
329
- schema = dumps (self .schema ), data = data
330
- )
350
+
331
351
return serialized
332
352
333
353
0 commit comments