5
5
import csv as csvlib
6
6
from io import StringIO
7
7
import os
8
- from typing import List
8
+ from typing import Hashable , List , Mapping , Optional , Sequence , Union
9
9
import warnings
10
10
from zipfile import ZipFile
11
11
12
12
import numpy as np
13
13
14
14
from pandas ._libs import writers as libwriters
15
+ from pandas ._typing import FilePathOrBuffer
15
16
16
17
from pandas .core .dtypes .generic import (
17
18
ABCDatetimeIndex ,
@@ -33,27 +34,26 @@ class CSVFormatter:
33
34
def __init__ (
34
35
self ,
35
36
obj ,
36
- path_or_buf = None ,
37
- sep = "," ,
38
- na_rep = "" ,
39
- float_format = None ,
37
+ path_or_buf : Optional [ FilePathOrBuffer [ str ]] = None ,
38
+ sep : str = "," ,
39
+ na_rep : str = "" ,
40
+ float_format : Optional [ str ] = None ,
40
41
cols = None ,
41
- header = True ,
42
- index = True ,
43
- index_label = None ,
44
- mode = "w" ,
45
- encoding = None ,
46
- compression = "infer" ,
47
- quoting = None ,
42
+ header : Union [ bool , Sequence [ Hashable ]] = True ,
43
+ index : bool = True ,
44
+ index_label : Optional [ Union [ bool , Hashable , Sequence [ Hashable ]]] = None ,
45
+ mode : str = "w" ,
46
+ encoding : Optional [ str ] = None ,
47
+ compression : Union [ str , Mapping [ str , str ], None ] = "infer" ,
48
+ quoting : Optional [ int ] = None ,
48
49
line_terminator = "\n " ,
49
- chunksize = None ,
50
+ chunksize : Optional [ int ] = None ,
50
51
quotechar = '"' ,
51
- date_format = None ,
52
- doublequote = True ,
53
- escapechar = None ,
52
+ date_format : Optional [ str ] = None ,
53
+ doublequote : bool = True ,
54
+ escapechar : Optional [ str ] = None ,
54
55
decimal = "." ,
55
56
):
56
-
57
57
self .obj = obj
58
58
59
59
if path_or_buf is None :
@@ -154,14 +154,17 @@ def __init__(
154
154
if not index :
155
155
self .nlevels = 0
156
156
157
- def save (self ):
157
+ def save (self ) -> None :
158
158
"""
159
- Create the writer & save
159
+ Create the writer & save.
160
160
"""
161
161
# GH21227 internal compression is not used when file-like passed.
162
162
if self .compression and hasattr (self .path_or_buf , "write" ):
163
- msg = "compression has no effect when passing file-like object as input."
164
- warnings .warn (msg , RuntimeWarning , stacklevel = 2 )
163
+ warnings .warn (
164
+ "compression has no effect when passing file-like object as input." ,
165
+ RuntimeWarning ,
166
+ stacklevel = 2 ,
167
+ )
165
168
166
169
# when zip compression is called.
167
170
is_zip = isinstance (self .path_or_buf , ZipFile ) or (
@@ -223,7 +226,6 @@ def save(self):
223
226
_fh .close ()
224
227
225
228
def _save_header (self ):
226
-
227
229
writer = self .writer
228
230
obj = self .obj
229
231
index_label = self .index_label
@@ -303,8 +305,7 @@ def _save_header(self):
303
305
encoded_labels .extend (["" ] * len (columns ))
304
306
writer .writerow (encoded_labels )
305
307
306
- def _save (self ):
307
-
308
+ def _save (self ) -> None :
308
309
self ._save_header ()
309
310
310
311
nrows = len (self .data_index )
@@ -321,8 +322,7 @@ def _save(self):
321
322
322
323
self ._save_chunk (start_i , end_i )
323
324
324
- def _save_chunk (self , start_i : int , end_i : int ):
325
-
325
+ def _save_chunk (self , start_i : int , end_i : int ) -> None :
326
326
data_index = self .data_index
327
327
328
328
# create the data for a chunk
0 commit comments