1
+ from __future__ import annotations
2
+
1
3
from abc import (
2
4
ABC ,
3
5
abstractmethod ,
10
12
Any ,
11
13
Callable ,
12
14
Mapping ,
13
- Optional ,
14
- Tuple ,
15
- Type ,
16
- Union ,
17
15
)
18
16
19
17
import numpy as np
78
76
def to_json (
79
77
path_or_buf ,
80
78
obj : NDFrame ,
81
- orient : Optional [ str ] = None ,
79
+ orient : str | None = None ,
82
80
date_format : str = "epoch" ,
83
81
double_precision : int = 10 ,
84
82
force_ascii : bool = True ,
85
83
date_unit : str = "ms" ,
86
- default_handler : Optional [ Callable [[Any ], JSONSerializable ]] = None ,
84
+ default_handler : Callable [[Any ], JSONSerializable ] | None = None ,
87
85
lines : bool = False ,
88
86
compression : CompressionOptions = "infer" ,
89
87
index : bool = True ,
@@ -102,7 +100,7 @@ def to_json(
102
100
if orient == "table" and isinstance (obj , Series ):
103
101
obj = obj .to_frame (name = obj .name or "values" )
104
102
105
- writer : Type [Writer ]
103
+ writer : type [Writer ]
106
104
if orient == "table" and isinstance (obj , DataFrame ):
107
105
writer = JSONTableWriter
108
106
elif isinstance (obj , Series ):
@@ -143,13 +141,13 @@ class Writer(ABC):
143
141
def __init__ (
144
142
self ,
145
143
obj ,
146
- orient : Optional [ str ] ,
144
+ orient : str | None ,
147
145
date_format : str ,
148
146
double_precision : int ,
149
147
ensure_ascii : bool ,
150
148
date_unit : str ,
151
149
index : bool ,
152
- default_handler : Optional [ Callable [[Any ], JSONSerializable ]] = None ,
150
+ default_handler : Callable [[Any ], JSONSerializable ] | None = None ,
153
151
indent : int = 0 ,
154
152
):
155
153
self .obj = obj
@@ -187,7 +185,7 @@ def write(self):
187
185
188
186
@property
189
187
@abstractmethod
190
- def obj_to_write (self ) -> Union [ NDFrame , Mapping [IndexLabel , Any ] ]:
188
+ def obj_to_write (self ) -> NDFrame | Mapping [IndexLabel , Any ]:
191
189
"""Object to write in JSON format."""
192
190
pass
193
191
@@ -196,7 +194,7 @@ class SeriesWriter(Writer):
196
194
_default_orient = "index"
197
195
198
196
@property
199
- def obj_to_write (self ) -> Union [ NDFrame , Mapping [IndexLabel , Any ] ]:
197
+ def obj_to_write (self ) -> NDFrame | Mapping [IndexLabel , Any ]:
200
198
if not self .index and self .orient == "split" :
201
199
return {"name" : self .obj .name , "data" : self .obj .values }
202
200
else :
@@ -211,7 +209,7 @@ class FrameWriter(Writer):
211
209
_default_orient = "columns"
212
210
213
211
@property
214
- def obj_to_write (self ) -> Union [ NDFrame , Mapping [IndexLabel , Any ] ]:
212
+ def obj_to_write (self ) -> NDFrame | Mapping [IndexLabel , Any ]:
215
213
if not self .index and self .orient == "split" :
216
214
obj_to_write = self .obj .to_dict (orient = "split" )
217
215
del obj_to_write ["index" ]
@@ -243,13 +241,13 @@ class JSONTableWriter(FrameWriter):
243
241
def __init__ (
244
242
self ,
245
243
obj ,
246
- orient : Optional [ str ] ,
244
+ orient : str | None ,
247
245
date_format : str ,
248
246
double_precision : int ,
249
247
ensure_ascii : bool ,
250
248
date_unit : str ,
251
249
index : bool ,
252
- default_handler : Optional [ Callable [[Any ], JSONSerializable ]] = None ,
250
+ default_handler : Callable [[Any ], JSONSerializable ] | None = None ,
253
251
indent : int = 0 ,
254
252
):
255
253
"""
@@ -313,7 +311,7 @@ def __init__(
313
311
self .index = index
314
312
315
313
@property
316
- def obj_to_write (self ) -> Union [ NDFrame , Mapping [IndexLabel , Any ] ]:
314
+ def obj_to_write (self ) -> NDFrame | Mapping [IndexLabel , Any ]:
317
315
return {"schema" : self .schema , "data" : self .obj }
318
316
319
317
@@ -326,19 +324,19 @@ def read_json(
326
324
path_or_buf = None ,
327
325
orient = None ,
328
326
typ = "frame" ,
329
- dtype : Optional [ DtypeArg ] = None ,
327
+ dtype : DtypeArg | None = None ,
330
328
convert_axes = None ,
331
329
convert_dates = True ,
332
330
keep_default_dates : bool = True ,
333
331
numpy : bool = False ,
334
332
precise_float : bool = False ,
335
333
date_unit = None ,
336
334
encoding = None ,
337
- encoding_errors : Optional [ str ] = "strict" ,
335
+ encoding_errors : str | None = "strict" ,
338
336
lines : bool = False ,
339
- chunksize : Optional [ int ] = None ,
337
+ chunksize : int | None = None ,
340
338
compression : CompressionOptions = "infer" ,
341
- nrows : Optional [ int ] = None ,
339
+ nrows : int | None = None ,
342
340
storage_options : StorageOptions = None ,
343
341
):
344
342
"""
@@ -639,11 +637,11 @@ def __init__(
639
637
date_unit ,
640
638
encoding ,
641
639
lines : bool ,
642
- chunksize : Optional [ int ] ,
640
+ chunksize : int | None ,
643
641
compression : CompressionOptions ,
644
- nrows : Optional [ int ] ,
642
+ nrows : int | None ,
645
643
storage_options : StorageOptions = None ,
646
- encoding_errors : Optional [ str ] = "strict" ,
644
+ encoding_errors : str | None = "strict" ,
647
645
):
648
646
649
647
self .orient = orient
@@ -663,7 +661,7 @@ def __init__(
663
661
self .nrows_seen = 0
664
662
self .nrows = nrows
665
663
self .encoding_errors = encoding_errors
666
- self .handles : Optional [ IOHandles ] = None
664
+ self .handles : IOHandles | None = None
667
665
668
666
if self .chunksize is not None :
669
667
self .chunksize = validate_integer ("chunksize" , self .chunksize , 1 )
@@ -816,7 +814,7 @@ def __exit__(self, exc_type, exc_value, traceback):
816
814
817
815
818
816
class Parser :
819
- _split_keys : Tuple [str , ...]
817
+ _split_keys : tuple [str , ...]
820
818
_default_orient : str
821
819
822
820
_STAMP_UNITS = ("s" , "ms" , "us" , "ns" )
@@ -831,7 +829,7 @@ def __init__(
831
829
self ,
832
830
json ,
833
831
orient ,
834
- dtype : Optional [ DtypeArg ] = None ,
832
+ dtype : DtypeArg | None = None ,
835
833
convert_axes = True ,
836
834
convert_dates = True ,
837
835
keep_default_dates = False ,
@@ -865,7 +863,7 @@ def __init__(
865
863
self .convert_dates = convert_dates
866
864
self .date_unit = date_unit
867
865
self .keep_default_dates = keep_default_dates
868
- self .obj : Optional [ FrameOrSeriesUnion ] = None
866
+ self .obj : FrameOrSeriesUnion | None = None
869
867
870
868
def check_keys_split (self , decoded ):
871
869
"""
0 commit comments