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 List , Optional , Sequence , Union
9
9
import warnings
10
10
from zipfile import ZipFile
11
11
21
21
)
22
22
from pandas .core .dtypes .missing import notna
23
23
24
+ from pandas ._typing import FilePathOrBuffer
25
+
24
26
from pandas .io .common import (
25
27
get_compression_method ,
26
28
get_filepath_or_buffer ,
30
32
31
33
32
34
class CSVFormatter :
35
+ r"""
36
+ Parameters
37
+ ----------
38
+ obj
39
+ path_or_buf : FilePathOrBuffer[str], optional
40
+ sep : str, default ','
41
+ na_rep : str, default ''
42
+ float_format : default None
43
+ cols : default None
44
+ header : Union[bool, Sequence[str]], default True
45
+ index : bool, default True
46
+ index_label : default None
47
+ mode : str, default 'w'
48
+ encoding : str, optional
49
+ compression : default 'infer'
50
+ quoting : int, optional
51
+ line_terminator : str, optional, default '\n'
52
+ chunksize : int, optional
53
+ quotechar : str, optional, default '"'
54
+ date_format : str, optional
55
+ doublequote : bool, default True
56
+ escapechar : default None
57
+ decimal : str, optional, default '.'
58
+ """
59
+
33
60
def __init__ (
34
61
self ,
35
62
obj ,
36
- path_or_buf = None ,
37
- sep = "," ,
38
- na_rep = "" ,
63
+ path_or_buf : Optional [ FilePathOrBuffer [ str ]] = None ,
64
+ sep : str = "," ,
65
+ na_rep : str = "" ,
39
66
float_format = None ,
40
67
cols = None ,
41
- header = True ,
42
- index = True ,
68
+ header : Union [ bool , Sequence [ str ]] = True ,
69
+ index : bool = True ,
43
70
index_label = None ,
44
- mode = "w" ,
45
- encoding = None ,
71
+ mode : str = "w" ,
72
+ encoding : Optional [ str ] = None ,
46
73
compression = "infer" ,
47
- quoting = None ,
48
- line_terminator = "\n " ,
49
- chunksize = None ,
50
- quotechar = '"' ,
51
- date_format = None ,
52
- doublequote = True ,
74
+ quoting : Optional [ int ] = None ,
75
+ line_terminator : Optional [ str ] = "\n " ,
76
+ chunksize : Optional [ int ] = None ,
77
+ quotechar : Optional [ str ] = '"' ,
78
+ date_format : Optional [ str ] = None ,
79
+ doublequote : bool = True ,
53
80
escapechar = None ,
54
- decimal = "." ,
81
+ decimal : Optional [ str ] = "." ,
55
82
):
56
-
57
83
self .obj = obj
58
84
59
85
if path_or_buf is None :
@@ -154,14 +180,17 @@ def __init__(
154
180
if not index :
155
181
self .nlevels = 0
156
182
157
- def save (self ):
183
+ def save (self ) -> None :
158
184
"""
159
- Create the writer & save
185
+ Create the writer & save.
160
186
"""
161
187
# GH21227 internal compression is not used when file-like passed.
162
188
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 )
189
+ warnings .warn (
190
+ "compression has no effect when passing file-like object as input." ,
191
+ RuntimeWarning ,
192
+ stacklevel = 2 ,
193
+ )
165
194
166
195
# when zip compression is called.
167
196
is_zip = isinstance (self .path_or_buf , ZipFile ) or (
@@ -223,7 +252,6 @@ def save(self):
223
252
_fh .close ()
224
253
225
254
def _save_header (self ):
226
-
227
255
writer = self .writer
228
256
obj = self .obj
229
257
index_label = self .index_label
@@ -306,8 +334,7 @@ def _save_header(self):
306
334
encoded_labels .extend (["" ] * len (columns ))
307
335
writer .writerow (encoded_labels )
308
336
309
- def _save (self ):
310
-
337
+ def _save (self ) -> None :
311
338
self ._save_header ()
312
339
313
340
nrows = len (self .data_index )
@@ -324,8 +351,13 @@ def _save(self):
324
351
325
352
self ._save_chunk (start_i , end_i )
326
353
327
- def _save_chunk (self , start_i : int , end_i : int ):
328
-
354
+ def _save_chunk (self , start_i : int , end_i : int ) -> None :
355
+ """
356
+ Parameters
357
+ ----------
358
+ start_i : int
359
+ end_i : int
360
+ """
329
361
data_index = self .data_index
330
362
331
363
# create the data for a chunk
0 commit comments