Skip to content

Commit 8f57389

Browse files
ShaharNavehproost
authored andcommitted
CLN:F-string formatting (pandas-dev#29554)
1 parent 20a4b3b commit 8f57389

File tree

3 files changed

+33
-44
lines changed

3 files changed

+33
-44
lines changed

pandas/_libs/parsers.pyx

+28-39
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,7 @@ cdef class TextReader:
589589

590590
if not isinstance(quote_char, (str, bytes)) and quote_char is not None:
591591
dtype = type(quote_char).__name__
592-
raise TypeError('"quotechar" must be string, '
593-
'not {dtype}'.format(dtype=dtype))
592+
raise TypeError(f'"quotechar" must be string, not {dtype}')
594593

595594
if quote_char is None or quote_char == '':
596595
if quoting != QUOTE_NONE:
@@ -685,7 +684,7 @@ cdef class TextReader:
685684
if not os.path.exists(source):
686685
raise FileNotFoundError(
687686
ENOENT,
688-
'File {source} does not exist'.format(source=source),
687+
f'File {source} does not exist',
689688
source)
690689
raise IOError('Initializing from file failed')
691690

@@ -741,8 +740,8 @@ cdef class TextReader:
741740
self.parser.lines < hr):
742741
msg = self.orig_header
743742
if isinstance(msg, list):
744-
msg = "[%s], len of %d," % (
745-
','.join(str(m) for m in msg), len(msg))
743+
joined = ','.join(str(m) for m in msg)
744+
msg = f"[{joined}], len of {len(msg)},"
746745
raise ParserError(
747746
f'Passed header={msg} but only '
748747
f'{self.parser.lines} lines in file')
@@ -768,10 +767,9 @@ cdef class TextReader:
768767

769768
if name == '':
770769
if self.has_mi_columns:
771-
name = ('Unnamed: {i}_level_{lvl}'
772-
.format(i=i, lvl=level))
770+
name = f'Unnamed: {i}_level_{level}'
773771
else:
774-
name = 'Unnamed: {i}'.format(i=i)
772+
name = f'Unnamed: {i}'
775773
unnamed_count += 1
776774

777775
count = counts.get(name, 0)
@@ -990,7 +988,7 @@ cdef class TextReader:
990988
cdef _end_clock(self, what):
991989
if self.verbose:
992990
elapsed = time.time() - self.clocks.pop(-1)
993-
print('%s took: %.2f ms' % (what, elapsed * 1000))
991+
print(f'{what} took: {elapsed * 1000:.2f} ms')
994992

995993
def set_noconvert(self, i):
996994
self.noconvert.add(i)
@@ -1028,11 +1026,9 @@ cdef class TextReader:
10281026
(num_cols >= self.parser.line_fields[i]) * num_cols
10291027

10301028
if self.table_width - self.leading_cols > num_cols:
1031-
raise ParserError(
1032-
"Too many columns specified: expected {expected} and "
1033-
"found {found}"
1034-
.format(expected=self.table_width - self.leading_cols,
1035-
found=num_cols))
1029+
raise ParserError(f"Too many columns specified: expected "
1030+
f"{self.table_width - self.leading_cols} "
1031+
f"and found {num_cols}")
10361032

10371033
results = {}
10381034
nused = 0
@@ -1075,9 +1071,9 @@ cdef class TextReader:
10751071

10761072
if conv:
10771073
if col_dtype is not None:
1078-
warnings.warn(("Both a converter and dtype were specified "
1079-
"for column {0} - only the converter will "
1080-
"be used").format(name), ParserWarning,
1074+
warnings.warn((f"Both a converter and dtype were specified "
1075+
f"for column {name} - only the converter will "
1076+
f"be used"), ParserWarning,
10811077
stacklevel=5)
10821078
results[i] = _apply_converter(conv, self.parser, i, start, end,
10831079
self.c_encoding)
@@ -1118,7 +1114,7 @@ cdef class TextReader:
11181114
col_res = _maybe_upcast(col_res)
11191115

11201116
if col_res is None:
1121-
raise ParserError('Unable to parse column {i}'.format(i=i))
1117+
raise ParserError(f'Unable to parse column {i}')
11221118

11231119
results[i] = col_res
11241120

@@ -1178,12 +1174,9 @@ cdef class TextReader:
11781174
col_res = col_res.astype(col_dtype)
11791175
if (col_res != col_res_orig).any():
11801176
raise ValueError(
1181-
"cannot safely convert passed user dtype of "
1182-
"{col_dtype} for {col_res} dtyped data in "
1183-
"column {column}".format(
1184-
col_dtype=col_dtype,
1185-
col_res=col_res_orig.dtype.name,
1186-
column=i))
1177+
f"cannot safely convert passed user dtype of "
1178+
f"{col_dtype} for {col_res_orig.dtype.name} dtyped data in "
1179+
f"column {i}")
11871180

11881181
return col_res, na_count
11891182

@@ -1216,9 +1209,9 @@ cdef class TextReader:
12161209
dtype=dtype)
12171210
except NotImplementedError:
12181211
raise NotImplementedError(
1219-
"Extension Array: {ea} must implement "
1220-
"_from_sequence_of_strings in order "
1221-
"to be used in parser methods".format(ea=array_type))
1212+
f"Extension Array: {array_type} must implement "
1213+
f"_from_sequence_of_strings in order "
1214+
f"to be used in parser methods")
12221215

12231216
return result, na_count
12241217

@@ -1228,8 +1221,7 @@ cdef class TextReader:
12281221
end, na_filter, na_hashset)
12291222
if user_dtype and na_count is not None:
12301223
if na_count > 0:
1231-
raise ValueError("Integer column has NA values in "
1232-
"column {column}".format(column=i))
1224+
raise ValueError(f"Integer column has NA values in column {i}")
12331225
except OverflowError:
12341226
result = _try_uint64(self.parser, i, start, end,
12351227
na_filter, na_hashset)
@@ -1253,8 +1245,7 @@ cdef class TextReader:
12531245
self.true_set, self.false_set)
12541246
if user_dtype and na_count is not None:
12551247
if na_count > 0:
1256-
raise ValueError("Bool column has NA values in "
1257-
"column {column}".format(column=i))
1248+
raise ValueError(f"Bool column has NA values in column {i}")
12581249
return result, na_count
12591250

12601251
elif dtype.kind == 'S':
@@ -1270,8 +1261,7 @@ cdef class TextReader:
12701261
elif dtype.kind == 'U':
12711262
width = dtype.itemsize
12721263
if width > 0:
1273-
raise TypeError("the dtype {dtype} is not "
1274-
"supported for parsing".format(dtype=dtype))
1264+
raise TypeError(f"the dtype {dtype} is not supported for parsing")
12751265

12761266
# unicode variable width
12771267
return self._string_convert(i, start, end, na_filter,
@@ -1280,12 +1270,11 @@ cdef class TextReader:
12801270
return self._string_convert(i, start, end, na_filter,
12811271
na_hashset)
12821272
elif is_datetime64_dtype(dtype):
1283-
raise TypeError("the dtype {dtype} is not supported "
1284-
"for parsing, pass this column "
1285-
"using parse_dates instead".format(dtype=dtype))
1273+
raise TypeError(f"the dtype {dtype} is not supported "
1274+
f"for parsing, pass this column "
1275+
f"using parse_dates instead")
12861276
else:
1287-
raise TypeError("the dtype {dtype} is not "
1288-
"supported for parsing".format(dtype=dtype))
1277+
raise TypeError(f"the dtype {dtype} is not supported for parsing")
12891278

12901279
cdef _string_convert(self, Py_ssize_t i, int64_t start, int64_t end,
12911280
bint na_filter, kh_str_starts_t *na_hashset):
@@ -2132,7 +2121,7 @@ cdef raise_parser_error(object base, parser_t *parser):
21322121
Py_XDECREF(type)
21332122
raise old_exc
21342123

2135-
message = '{base}. C error: '.format(base=base)
2124+
message = f'{base}. C error: '
21362125
if parser.error_msg != NULL:
21372126
message += parser.error_msg.decode('utf-8')
21382127
else:

pandas/_libs/testing.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,12 @@ cpdef assert_almost_equal(a, b,
204204
# case for zero
205205
if abs(fa) < 1e-5:
206206
if not decimal_almost_equal(fa, fb, decimal):
207-
assert False, ('(very low values) expected %.5f but '
208-
'got %.5f, with decimal %d' % (fb, fa, decimal))
207+
assert False, (f'(very low values) expected {fb:.5f} '
208+
f'but got {fa:.5f}, with decimal {decimal}')
209209
else:
210210
if not decimal_almost_equal(1, fb / fa, decimal):
211-
assert False, ('expected %.5f but got %.5f, '
212-
'with decimal %d' % (fb, fa, decimal))
211+
assert False, (f'expected {fb:.5f} but got {fa:.5f}, '
212+
f'with decimal {decimal}')
213213
return True
214214

215215
raise AssertionError(f"{a} != {b}")

pandas/_libs/tslib.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def format_array_from_datetime(ndarray[int64_t] values, object tz=None,
279279
elif show_us:
280280
res += '.%.6d' % dts.us
281281
elif show_ms:
282-
res += '.%.3d' % (dts.us /1000)
282+
res += '.%.3d' % (dts.us / 1000)
283283

284284
result[i] = res
285285

0 commit comments

Comments
 (0)