Skip to content

Commit bd78785

Browse files
naomi172839jreback
authored andcommitted
STYLE: Use f-string in io/parsers (#30466)
1 parent 8818407 commit bd78785

File tree

1 file changed

+41
-83
lines changed

1 file changed

+41
-83
lines changed

pandas/io/parsers.py

+41-83
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,7 @@ def _validate_integer(name, val, min_val=0):
381381
min_val : int
382382
Minimum allowed value (val < min_val will result in a ValueError)
383383
"""
384-
msg = "'{name:s}' must be an integer >={min_val:d}".format(
385-
name=name, min_val=min_val
386-
)
384+
msg = f"'{name:s}' must be an integer >={min_val:d}"
387385

388386
if val is not None:
389387
if is_float(val):
@@ -822,11 +820,7 @@ def __init__(self, f, engine=None, **kwds):
822820
try:
823821
dialect_val = getattr(dialect, param)
824822
except AttributeError:
825-
raise ValueError(
826-
"Invalid dialect '{dialect}' provided".format(
827-
dialect=kwds["dialect"]
828-
)
829-
)
823+
raise ValueError(f"Invalid dialect {kwds['dialect']} provided")
830824
parser_default = _parser_defaults[param]
831825
provided = kwds.get(param, parser_default)
832826

@@ -838,11 +832,9 @@ def __init__(self, f, engine=None, **kwds):
838832
# even if it conflicts with the dialect (gh-23761).
839833
if provided != parser_default and provided != dialect_val:
840834
msg = (
841-
"Conflicting values for '{param}': '{val}' was "
842-
"provided, but the dialect specifies '{diaval}'. "
843-
"Using the dialect-specified value.".format(
844-
param=param, val=provided, diaval=dialect_val
845-
)
835+
f"Conflicting values for '{param}': '{provided}' was "
836+
f"provided, but the dialect specifies '{dialect_val}'. "
837+
"Using the dialect-specified value."
846838
)
847839

848840
# Annoying corner case for not warning about
@@ -993,9 +985,9 @@ def _clean_options(self, options, engine):
993985
encodeable = False
994986
if not encodeable and engine not in ("python", "python-fwf"):
995987
fallback_reason = (
996-
"the separator encoded in {encoding} "
988+
f"the separator encoded in {encoding} "
997989
"is > 1 char long, and the 'c' engine "
998-
"does not support such separators".format(encoding=encoding)
990+
"does not support such separators"
999991
)
1000992
engine = "python"
1001993

@@ -1025,19 +1017,19 @@ def _clean_options(self, options, engine):
10251017
for arg in _python_unsupported:
10261018
if fallback_reason and result[arg] != _c_parser_defaults[arg]:
10271019
raise ValueError(
1028-
f"Falling back to the 'python' engine because "
1020+
"Falling back to the 'python' engine because "
10291021
f"{fallback_reason}, but this causes {repr(arg)} to be "
1030-
f"ignored as it is not supported by the 'python' engine."
1022+
"ignored as it is not supported by the 'python' engine."
10311023
)
10321024
del result[arg]
10331025

10341026
if fallback_reason:
10351027
warnings.warn(
10361028
(
10371029
"Falling back to the 'python' engine because "
1038-
"{0}; you can avoid this warning by specifying "
1030+
f"{fallback_reason}; you can avoid this warning by specifying "
10391031
"engine='python'."
1040-
).format(fallback_reason),
1032+
),
10411033
ParserWarning,
10421034
stacklevel=5,
10431035
)
@@ -1058,7 +1050,7 @@ def _clean_options(self, options, engine):
10581050

10591051
msg = (
10601052
f"The {repr(arg)} argument has been deprecated and will be "
1061-
f"removed in a future version."
1053+
"removed in a future version."
10621054
)
10631055

10641056
if result.get(arg, depr_default) != depr_default:
@@ -1128,9 +1120,9 @@ def _make_engine(self, engine="c"):
11281120
klass = FixedWidthFieldParser
11291121
else:
11301122
raise ValueError(
1131-
"Unknown engine: {engine} (valid options are"
1123+
f"Unknown engine: {engine} (valid options are"
11321124
' "c", "python", or'
1133-
' "python-fwf")'.format(engine=engine)
1125+
' "python-fwf")'
11341126
)
11351127
self._engine = klass(self.f, **self.options)
11361128

@@ -1240,7 +1232,7 @@ def _validate_usecols_names(usecols, names):
12401232
if len(missing) > 0:
12411233
raise ValueError(
12421234
"Usecols do not match columns, "
1243-
"columns expected but not found: {missing}".format(missing=missing)
1235+
f"columns expected but not found: {missing}"
12441236
)
12451237

12461238
return usecols
@@ -1541,11 +1533,9 @@ def _maybe_dedup_names(self, names):
15411533
counts[col] = cur_count + 1
15421534

15431535
if is_potential_mi:
1544-
col = col[:-1] + (
1545-
"{column}.{count}".format(column=col[-1], count=cur_count),
1546-
)
1536+
col = col[:-1] + (f"{col[-1]}.{cur_count}",)
15471537
else:
1548-
col = "{column}.{count}".format(column=col, count=cur_count)
1538+
col = f"{col}.{cur_count}"
15491539
cur_count = counts[col]
15501540

15511541
names[i] = col
@@ -1591,7 +1581,7 @@ def _get_simple_index(self, data, columns):
15911581
def ix(col):
15921582
if not isinstance(col, str):
15931583
return col
1594-
raise ValueError("Index {col} invalid".format(col=col))
1584+
raise ValueError(f"Index {col} invalid")
15951585

15961586
to_remove = []
15971587
index = []
@@ -1615,11 +1605,7 @@ def _get_name(icol):
16151605
return icol
16161606

16171607
if col_names is None:
1618-
raise ValueError(
1619-
("Must supply column order to use {icol!s} as index").format(
1620-
icol=icol
1621-
)
1622-
)
1608+
raise ValueError(f"Must supply column order to use {icol!s} as index")
16231609

16241610
for i, c in enumerate(col_names):
16251611
if i == icol:
@@ -1695,9 +1681,9 @@ def _convert_to_ndarrays(
16951681
warnings.warn(
16961682
(
16971683
"Both a converter and dtype were specified "
1698-
"for column {0} - only the converter will "
1684+
f"for column {c} - only the converter will "
16991685
"be used"
1700-
).format(c),
1686+
),
17011687
ParserWarning,
17021688
stacklevel=7,
17031689
)
@@ -1735,22 +1721,15 @@ def _convert_to_ndarrays(
17351721
and not is_categorical_dtype(cast_type)
17361722
and na_count > 0
17371723
):
1738-
raise ValueError(
1739-
"Bool column has NA values in "
1740-
"column {column}".format(column=c)
1741-
)
1724+
raise ValueError(f"Bool column has NA values in column {c}")
17421725
except (AttributeError, TypeError):
17431726
# invalid input to is_bool_dtype
17441727
pass
17451728
cvals = self._cast_types(cvals, cast_type, c)
17461729

17471730
result[c] = cvals
17481731
if verbose and na_count:
1749-
print(
1750-
"Filled {count} NA values in column {c!s}".format(
1751-
count=na_count, c=c
1752-
)
1753-
)
1732+
print(f"Filled {na_count} NA values in column {c!s}")
17541733
return result
17551734

17561735
def _infer_types(self, values, na_values, try_num_bool=True):
@@ -1847,18 +1826,17 @@ def _cast_types(self, values, cast_type, column):
18471826
return array_type._from_sequence_of_strings(values, dtype=cast_type)
18481827
except NotImplementedError:
18491828
raise NotImplementedError(
1850-
"Extension Array: {ea} must implement "
1829+
f"Extension Array: {array_type} must implement "
18511830
"_from_sequence_of_strings in order "
1852-
"to be used in parser methods".format(ea=array_type)
1831+
"to be used in parser methods"
18531832
)
18541833

18551834
else:
18561835
try:
18571836
values = astype_nansafe(values, cast_type, copy=True, skipna=True)
18581837
except ValueError:
18591838
raise ValueError(
1860-
"Unable to convert column {column} to type "
1861-
"{cast_type}".format(column=column, cast_type=cast_type)
1839+
f"Unable to convert column {column} to type {cast_type}"
18621840
)
18631841
return values
18641842

@@ -1929,8 +1907,7 @@ def __init__(self, src, **kwds):
19291907
if self.names is None:
19301908
if self.prefix:
19311909
self.names = [
1932-
"{prefix}{i}".format(prefix=self.prefix, i=i)
1933-
for i in range(self._reader.table_width)
1910+
f"{self.prefix}{i}" for i in range(self._reader.table_width)
19341911
]
19351912
else:
19361913
self.names = list(range(self._reader.table_width))
@@ -2345,15 +2322,9 @@ def __init__(self, f, **kwds):
23452322
raise ValueError("Only length-1 decimal markers supported")
23462323

23472324
if self.thousands is None:
2348-
self.nonnum = re.compile(
2349-
r"[^-^0-9^{decimal}]+".format(decimal=self.decimal)
2350-
)
2325+
self.nonnum = re.compile(fr"[^-^0-9^{self.decimal}]+")
23512326
else:
2352-
self.nonnum = re.compile(
2353-
r"[^-^0-9^{thousands}^{decimal}]+".format(
2354-
thousands=self.thousands, decimal=self.decimal
2355-
)
2356-
)
2327+
self.nonnum = re.compile(fr"[^-^0-9^{self.thousands}^{self.decimal}]+")
23572328

23582329
def _set_no_thousands_columns(self):
23592330
# Create a set of column ids that are not to be stripped of thousands
@@ -2589,8 +2560,8 @@ def _infer_columns(self):
25892560
except StopIteration:
25902561
if self.line_pos < hr:
25912562
raise ValueError(
2592-
"Passed header={hr} but only {pos} lines in "
2593-
"file".format(hr=hr, pos=(self.line_pos + 1))
2563+
f"Passed header={hr} but only {self.line_pos + 1} lines in "
2564+
"file"
25942565
)
25952566

25962567
# We have an empty file, so check
@@ -2613,11 +2584,9 @@ def _infer_columns(self):
26132584
for i, c in enumerate(line):
26142585
if c == "":
26152586
if have_mi_columns:
2616-
col_name = "Unnamed: {i}_level_{level}".format(
2617-
i=i, level=level
2618-
)
2587+
col_name = f"Unnamed: {i}_level_{level}"
26192588
else:
2620-
col_name = "Unnamed: {i}".format(i=i)
2589+
col_name = f"Unnamed: {i}"
26212590

26222591
this_unnamed_cols.append(i)
26232592
this_columns.append(col_name)
@@ -2632,7 +2601,7 @@ def _infer_columns(self):
26322601

26332602
while cur_count > 0:
26342603
counts[col] = cur_count + 1
2635-
col = "{column}.{count}".format(column=col, count=cur_count)
2604+
col = f"{col}.{cur_count}"
26362605
cur_count = counts[col]
26372606

26382607
this_columns[i] = col
@@ -2697,12 +2666,7 @@ def _infer_columns(self):
26972666

26982667
if not names:
26992668
if self.prefix:
2700-
columns = [
2701-
[
2702-
"{prefix}{idx}".format(prefix=self.prefix, idx=i)
2703-
for i in range(ncols)
2704-
]
2705-
]
2669+
columns = [[f"{self.prefix}{i}" for i in range(ncols)]]
27062670
else:
27072671
columns = [list(range(ncols))]
27082672
columns = self._handle_usecols(columns, columns[0])
@@ -2904,7 +2868,7 @@ def _alert_malformed(self, msg, row_num):
29042868
if self.error_bad_lines:
29052869
raise ParserError(msg)
29062870
elif self.warn_bad_lines:
2907-
base = "Skipping line {row_num}: ".format(row_num=row_num)
2871+
base = f"Skipping line {row_num}: "
29082872
sys.stderr.write(base + msg + "\n")
29092873

29102874
def _next_iter_line(self, row_num):
@@ -3128,10 +3092,8 @@ def _rows_to_cols(self, content):
31283092

31293093
for row_num, actual_len in bad_lines:
31303094
msg = (
3131-
"Expected {col_len} fields in line {line}, saw "
3132-
"{length}".format(
3133-
col_len=col_len, line=(row_num + 1), length=actual_len
3134-
)
3095+
f"Expected {col_len} fields in line {row_num + 1}, saw "
3096+
f"{actual_len}"
31353097
)
31363098
if (
31373099
self.delimiter
@@ -3329,9 +3291,7 @@ def _isindex(colspec):
33293291
converter, colspec, data_dict, orig_names
33303292
)
33313293
if new_name in data_dict:
3332-
raise ValueError(
3333-
"New date column already in dict {name}".format(name=new_name)
3334-
)
3294+
raise ValueError(f"New date column already in dict {new_name}")
33353295
new_data[new_name] = col
33363296
new_cols.append(new_name)
33373297
date_cols.update(old_names)
@@ -3340,9 +3300,7 @@ def _isindex(colspec):
33403300
# dict of new name to column list
33413301
for new_name, colspec in parse_spec.items():
33423302
if new_name in data_dict:
3343-
raise ValueError(
3344-
"Date column {name} already in dict".format(name=new_name)
3345-
)
3303+
raise ValueError(f"Date column {new_name} already in dict")
33463304

33473305
_, col, old_names = _try_convert_dates(
33483306
converter, colspec, data_dict, orig_names
@@ -3521,7 +3479,7 @@ def _stringify_na_values(na_values):
35213479
# we are like 999 here
35223480
if v == int(v):
35233481
v = int(v)
3524-
result.append("{value}.0".format(value=v))
3482+
result.append(f"{v}.0")
35253483
result.append(str(v))
35263484

35273485
result.append(v)

0 commit comments

Comments
 (0)