Skip to content

Commit f183b8f

Browse files
jbrockmendelproost
authored andcommitted
CLN: annotations and docstrings in pd.io (pandas-dev#29605)
1 parent 17eebdb commit f183b8f

File tree

13 files changed

+172
-126
lines changed

13 files changed

+172
-126
lines changed

pandas/io/clipboard/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ def _stringifyText(text):
9595
acceptedTypes = (str, int, float, bool)
9696
if not isinstance(text, acceptedTypes):
9797
raise PyperclipException(
98-
"only str, int, float, and bool values"
99-
"can be copied to the clipboard, not".format(text.__class__.__name__)
98+
f"only str, int, float, and bool values"
99+
f"can be copied to the clipboard, not {text.__class__.__name__}"
100100
)
101101
return str(text)
102102

pandas/io/date_converters.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ def _check_columns(cols):
5757
for i, n in enumerate(map(len, tail)):
5858
if n != N:
5959
raise AssertionError(
60-
"All columns must have the same length: {0}; "
61-
"column {1} has length {2}".format(N, i, n)
60+
f"All columns must have the same length: {N}; "
61+
f"column {i} has length {n}"
6262
)
6363

6464
return N

pandas/io/excel/_odfreader.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def get_sheet_by_name(self, name: str):
6161
if table.getAttribute("name") == name:
6262
return table
6363

64-
raise ValueError("sheet {} not found".format(name))
64+
raise ValueError(f"sheet {name} not found")
6565

6666
def get_sheet_data(self, sheet, convert_float: bool) -> List[List[Scalar]]:
6767
"""Parse an ODF Table into a list of lists

pandas/io/excel/_openpyxl.py

+24-8
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def save(self):
4646
@classmethod
4747
def _convert_to_style(cls, style_dict):
4848
"""
49-
converts a style_dict to an openpyxl style object
49+
Converts a style_dict to an openpyxl style object.
50+
5051
Parameters
5152
----------
5253
style_dict : style dictionary to convert
@@ -72,7 +73,8 @@ def _convert_to_style(cls, style_dict):
7273
def _convert_to_style_kwargs(cls, style_dict):
7374
"""
7475
Convert a style_dict to a set of kwargs suitable for initializing
75-
or updating-on-copy an openpyxl v2 style object
76+
or updating-on-copy an openpyxl v2 style object.
77+
7678
Parameters
7779
----------
7880
style_dict : dict
@@ -83,6 +85,7 @@ def _convert_to_style_kwargs(cls, style_dict):
8385
'alignment'
8486
'number_format'
8587
'protection'
88+
8689
Returns
8790
-------
8891
style_kwargs : dict
@@ -107,7 +110,8 @@ def _convert_to_style_kwargs(cls, style_dict):
107110
@classmethod
108111
def _convert_to_color(cls, color_spec):
109112
"""
110-
Convert ``color_spec`` to an openpyxl v2 Color object
113+
Convert ``color_spec`` to an openpyxl v2 Color object.
114+
111115
Parameters
112116
----------
113117
color_spec : str, dict
@@ -120,6 +124,7 @@ def _convert_to_color(cls, color_spec):
120124
'tint'
121125
'index'
122126
'type'
127+
123128
Returns
124129
-------
125130
color : openpyxl.styles.Color
@@ -135,7 +140,8 @@ def _convert_to_color(cls, color_spec):
135140
@classmethod
136141
def _convert_to_font(cls, font_dict):
137142
"""
138-
Convert ``font_dict`` to an openpyxl v2 Font object
143+
Convert ``font_dict`` to an openpyxl v2 Font object.
144+
139145
Parameters
140146
----------
141147
font_dict : dict
@@ -154,6 +160,7 @@ def _convert_to_font(cls, font_dict):
154160
'outline'
155161
'shadow'
156162
'condense'
163+
157164
Returns
158165
-------
159166
font : openpyxl.styles.Font
@@ -185,11 +192,13 @@ def _convert_to_stop(cls, stop_seq):
185192
"""
186193
Convert ``stop_seq`` to a list of openpyxl v2 Color objects,
187194
suitable for initializing the ``GradientFill`` ``stop`` parameter.
195+
188196
Parameters
189197
----------
190198
stop_seq : iterable
191199
An iterable that yields objects suitable for consumption by
192200
``_convert_to_color``.
201+
193202
Returns
194203
-------
195204
stop : list of openpyxl.styles.Color
@@ -200,7 +209,8 @@ def _convert_to_stop(cls, stop_seq):
200209
@classmethod
201210
def _convert_to_fill(cls, fill_dict):
202211
"""
203-
Convert ``fill_dict`` to an openpyxl v2 Fill object
212+
Convert ``fill_dict`` to an openpyxl v2 Fill object.
213+
204214
Parameters
205215
----------
206216
fill_dict : dict
@@ -216,6 +226,7 @@ def _convert_to_fill(cls, fill_dict):
216226
'top'
217227
'bottom'
218228
'stop'
229+
219230
Returns
220231
-------
221232
fill : openpyxl.styles.Fill
@@ -262,14 +273,16 @@ def _convert_to_fill(cls, fill_dict):
262273
@classmethod
263274
def _convert_to_side(cls, side_spec):
264275
"""
265-
Convert ``side_spec`` to an openpyxl v2 Side object
276+
Convert ``side_spec`` to an openpyxl v2 Side object.
277+
266278
Parameters
267279
----------
268280
side_spec : str, dict
269281
A string specifying the border style, or a dict with zero or more
270282
of the following keys (or their synonyms).
271283
'style' ('border_style')
272284
'color'
285+
273286
Returns
274287
-------
275288
side : openpyxl.styles.Side
@@ -295,7 +308,8 @@ def _convert_to_side(cls, side_spec):
295308
@classmethod
296309
def _convert_to_border(cls, border_dict):
297310
"""
298-
Convert ``border_dict`` to an openpyxl v2 Border object
311+
Convert ``border_dict`` to an openpyxl v2 Border object.
312+
299313
Parameters
300314
----------
301315
border_dict : dict
@@ -311,6 +325,7 @@ def _convert_to_border(cls, border_dict):
311325
'diagonalUp' ('diagonalup')
312326
'diagonalDown' ('diagonaldown')
313327
'outline'
328+
314329
Returns
315330
-------
316331
border : openpyxl.styles.Border
@@ -335,7 +350,8 @@ def _convert_to_border(cls, border_dict):
335350
@classmethod
336351
def _convert_to_alignment(cls, alignment_dict):
337352
"""
338-
Convert ``alignment_dict`` to an openpyxl v2 Alignment object
353+
Convert ``alignment_dict`` to an openpyxl v2 Alignment object.
354+
339355
Parameters
340356
----------
341357
alignment_dict : dict

pandas/io/excel/_xlwt.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ def write_cells(
7777
wks.write(startrow + cell.row, startcol + cell.col, val, style)
7878

7979
@classmethod
80-
def _style_to_xlwt(cls, item, firstlevel=True, field_sep=",", line_sep=";"):
80+
def _style_to_xlwt(
81+
cls, item, firstlevel: bool = True, field_sep=",", line_sep=";"
82+
) -> str:
8183
"""helper which recursively generate an xlwt easy style string
8284
for example:
8385
@@ -117,6 +119,7 @@ def _style_to_xlwt(cls, item, firstlevel=True, field_sep=",", line_sep=";"):
117119
def _convert_to_style(cls, style_dict, num_format_str=None):
118120
"""
119121
converts a style_dict to an xlwt style object
122+
120123
Parameters
121124
----------
122125
style_dict : style dictionary to convert

pandas/io/feather_format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pandas.io.common import _stringify_path
1111

1212

13-
def to_feather(df, path):
13+
def to_feather(df: DataFrame, path):
1414
"""
1515
Write a DataFrame to the feather-format
1616

pandas/io/formats/csvs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ def _save(self):
327327

328328
self._save_chunk(start_i, end_i)
329329

330-
def _save_chunk(self, start_i, end_i):
330+
def _save_chunk(self, start_i: int, end_i: int):
331331

332332
data_index = self.data_index
333333

pandas/io/formats/excel.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ def __init__(self, inherited=None):
6363

6464
compute_css = CSSResolver()
6565

66-
def __call__(self, declarations_str):
67-
"""Convert CSS declarations to ExcelWriter style
66+
def __call__(self, declarations_str: str):
67+
"""
68+
Convert CSS declarations to ExcelWriter style.
6869
6970
Parameters
7071
----------
@@ -279,6 +280,7 @@ def build_font(self, props):
279280
if "text-shadow" in props
280281
else None
281282
),
283+
# FIXME: dont leave commented-out
282284
# 'vertAlign':,
283285
# 'charset': ,
284286
# 'scheme': ,
@@ -665,7 +667,7 @@ def _format_hierarchical_rows(self):
665667
for cell in self._generate_body(gcolidx):
666668
yield cell
667669

668-
def _generate_body(self, coloffset):
670+
def _generate_body(self, coloffset: int):
669671
if self.styler is None:
670672
styles = None
671673
else:

pandas/io/formats/latex.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212

1313
class LatexFormatter(TableFormatter):
14-
""" Used to render a DataFrame to a LaTeX tabular/longtable environment
15-
output.
14+
"""
15+
Used to render a DataFrame to a LaTeX tabular/longtable environment output.
1616
1717
Parameters
1818
----------
@@ -106,18 +106,19 @@ def pad_empties(x):
106106
# Get rid of old multiindex column and add new ones
107107
strcols = out + strcols[1:]
108108

109-
column_format = self.column_format
110-
if column_format is None:
109+
if self.column_format is None:
111110
dtypes = self.frame.dtypes._values
112111
column_format = "".join(map(get_col_type, dtypes))
113112
if self.fmt.index:
114113
index_format = "l" * self.frame.index.nlevels
115114
column_format = index_format + column_format
116-
elif not isinstance(column_format, str): # pragma: no cover
115+
elif not isinstance(self.column_format, str): # pragma: no cover
117116
raise AssertionError(
118117
"column_format must be str or unicode, "
119118
"not {typ}".format(typ=type(column_format))
120119
)
120+
else:
121+
column_format = self.column_format
121122

122123
if self.longtable:
123124
self._write_longtable_begin(buf, column_format)
@@ -265,15 +266,15 @@ def _format_multirow(
265266

266267
def _print_cline(self, buf: IO[str], i: int, icol: int) -> None:
267268
"""
268-
Print clines after multirow-blocks are finished
269+
Print clines after multirow-blocks are finished.
269270
"""
270271
for cl in self.clinebuf:
271272
if cl[0] == i:
272273
buf.write("\\cline{{{cl:d}-{icol:d}}}\n".format(cl=cl[1], icol=icol))
273274
# remove entries that have been written to buffer
274275
self.clinebuf = [x for x in self.clinebuf if x[0] != i]
275276

276-
def _write_tabular_begin(self, buf, column_format):
277+
def _write_tabular_begin(self, buf, column_format: str):
277278
"""
278279
Write the beginning of a tabular environment or
279280
nested table/tabular environments including caption and label.
@@ -283,11 +284,10 @@ def _write_tabular_begin(self, buf, column_format):
283284
buf : string or file handle
284285
File path or object. If not specified, the result is returned as
285286
a string.
286-
column_format : str, default None
287+
column_format : str
287288
The columns format as specified in `LaTeX table format
288289
<https://en.wikibooks.org/wiki/LaTeX/Tables>`__ e.g 'rcl'
289290
for 3 columns
290-
291291
"""
292292
if self.caption is not None or self.label is not None:
293293
# then write output in a nested table/tabular environment
@@ -327,7 +327,7 @@ def _write_tabular_end(self, buf):
327327
else:
328328
pass
329329

330-
def _write_longtable_begin(self, buf, column_format):
330+
def _write_longtable_begin(self, buf, column_format: str):
331331
"""
332332
Write the beginning of a longtable environment including caption and
333333
label if provided by user.
@@ -337,11 +337,10 @@ def _write_longtable_begin(self, buf, column_format):
337337
buf : string or file handle
338338
File path or object. If not specified, the result is returned as
339339
a string.
340-
column_format : str, default None
340+
column_format : str
341341
The columns format as specified in `LaTeX table format
342342
<https://en.wikibooks.org/wiki/LaTeX/Tables>`__ e.g 'rcl'
343343
for 3 columns
344-
345344
"""
346345
buf.write("\\begin{{longtable}}{{{fmt}}}\n".format(fmt=column_format))
347346

pandas/io/html.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,15 @@ def _importers():
5757
_RE_WHITESPACE = re.compile(r"[\r\n]+|\s{2,}")
5858

5959

60-
def _remove_whitespace(s, regex=_RE_WHITESPACE):
60+
def _remove_whitespace(s: str, regex=_RE_WHITESPACE) -> str:
6161
"""
6262
Replace extra whitespace inside of a string with a single space.
6363
6464
Parameters
6565
----------
6666
s : str or unicode
6767
The string from which to remove extra whitespace.
68-
69-
regex : regex
68+
regex : re.Pattern
7069
The regular expression to use to remove extra whitespace.
7170
7271
Returns
@@ -253,7 +252,8 @@ def _text_getter(self, obj):
253252
raise AbstractMethodError(self)
254253

255254
def _parse_td(self, obj):
256-
"""Return the td elements from a row element.
255+
"""
256+
Return the td elements from a row element.
257257
258258
Parameters
259259
----------
@@ -600,7 +600,7 @@ def _build_doc(self):
600600
)
601601

602602

603-
def _build_xpath_expr(attrs):
603+
def _build_xpath_expr(attrs) -> str:
604604
"""Build an xpath expression to simulate bs4's ability to pass in kwargs to
605605
search for attributes when using the lxml parser.
606606
@@ -810,7 +810,8 @@ def _data_to_frame(**kwargs):
810810

811811

812812
def _parser_dispatch(flavor):
813-
"""Choose the parser based on the input flavor.
813+
"""
814+
Choose the parser based on the input flavor.
814815
815816
Parameters
816817
----------
@@ -850,7 +851,7 @@ def _parser_dispatch(flavor):
850851
return _valid_parsers[flavor]
851852

852853

853-
def _print_as_set(s):
854+
def _print_as_set(s) -> str:
854855
return "{" + "{arg}".format(arg=", ".join(pprint_thing(el) for el in s)) + "}"
855856

856857

0 commit comments

Comments
 (0)