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