Skip to content

Commit 07ee00e

Browse files
bharatr21jreback
authored andcommitted
CLN: Use fstring instead of .format in io/excel and test/generic (#30601)
1 parent 2b0d1a1 commit 07ee00e

File tree

8 files changed

+22
-25
lines changed

8 files changed

+22
-25
lines changed

pandas/io/excel/_base.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,7 @@ def read_excel(
297297

298298
for arg in ("sheet", "sheetname", "parse_cols"):
299299
if arg in kwds:
300-
raise TypeError(
301-
"read_excel() got an unexpected keyword argument `{}`".format(arg)
302-
)
300+
raise TypeError(f"read_excel() got an unexpected keyword argument `{arg}`")
303301

304302
if not isinstance(io, ExcelFile):
305303
io = ExcelFile(io, engine=engine)
@@ -429,7 +427,7 @@ def parse(
429427

430428
for asheetname in sheets:
431429
if verbose:
432-
print("Reading sheet {sheet}".format(sheet=asheetname))
430+
print(f"Reading sheet {asheetname}")
433431

434432
if isinstance(asheetname, str):
435433
sheet = self.get_sheet_by_name(asheetname)
@@ -622,11 +620,11 @@ def __new__(cls, path, engine=None, **kwargs):
622620
ext = "xlsx"
623621

624622
try:
625-
engine = config.get_option("io.excel.{ext}.writer".format(ext=ext))
623+
engine = config.get_option(f"io.excel.{ext}.writer")
626624
if engine == "auto":
627625
engine = _get_default_writer(ext)
628626
except KeyError:
629-
raise ValueError("No engine for filetype: '{ext}'".format(ext=ext))
627+
raise ValueError(f"No engine for filetype: '{ext}'")
630628
cls = get_writer(engine)
631629

632630
return object.__new__(cls)
@@ -757,9 +755,8 @@ def check_extension(cls, ext):
757755
if ext.startswith("."):
758756
ext = ext[1:]
759757
if not any(ext in extension for extension in cls.supported_extensions):
760-
msg = "Invalid extension for engine '{engine}': '{ext}'".format(
761-
engine=pprint_thing(cls.engine), ext=pprint_thing(ext)
762-
)
758+
msg = "Invalid extension for engine"
759+
f"'{pprint_thing(cls.engine)}': '{pprint_thing(ext)}'"
763760
raise ValueError(msg)
764761
else:
765762
return True
@@ -802,7 +799,7 @@ def __init__(self, io, engine=None):
802799
if engine is None:
803800
engine = "xlrd"
804801
if engine not in self._engines:
805-
raise ValueError("Unknown engine: {engine}".format(engine=engine))
802+
raise ValueError(f"Unknown engine: {engine}")
806803

807804
self.engine = engine
808805
# could be a str, ExcelFile, Book, etc.

pandas/io/excel/_odfreader.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,4 @@ def _get_cell_value(self, cell, convert_float: bool) -> Scalar:
178178
elif cell_type == "time":
179179
return pd.to_datetime(str(cell)).time()
180180
else:
181-
raise ValueError("Unrecognized type {}".format(cell_type))
181+
raise ValueError(f"Unrecognized type {cell_type}")

pandas/io/excel/_openpyxl.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def _convert_to_style_kwargs(cls, style_dict):
9999
for k, v in style_dict.items():
100100
if k in _style_key_map:
101101
k = _style_key_map[k]
102-
_conv_to_x = getattr(cls, "_convert_to_{k}".format(k=k), lambda x: None)
102+
_conv_to_x = getattr(cls, f"_convert_to_{k}", lambda x: None)
103103
new_v = _conv_to_x(v)
104104
if new_v:
105105
style_kwargs[k] = new_v

pandas/io/excel/_util.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def get_writer(engine_name):
4848
try:
4949
return _writers[engine_name]
5050
except KeyError:
51-
raise ValueError("No Excel writer '{engine}'".format(engine=engine_name))
51+
raise ValueError(f"No Excel writer '{engine_name}'")
5252

5353

5454
def _excel2num(x):
@@ -76,7 +76,7 @@ def _excel2num(x):
7676
cp = ord(c)
7777

7878
if cp < ord("A") or cp > ord("Z"):
79-
raise ValueError("Invalid column name: {x}".format(x=x))
79+
raise ValueError(f"Invalid column name: {x}")
8080

8181
index = index * 26 + cp - ord("A") + 1
8282

pandas/io/excel/_xlwt.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,20 @@ def _style_to_xlwt(
9797
if hasattr(item, "items"):
9898
if firstlevel:
9999
it = [
100-
"{key}: {val}".format(key=key, val=cls._style_to_xlwt(value, False))
100+
f"{key}: {cls._style_to_xlwt(value, False)}"
101101
for key, value in item.items()
102102
]
103-
out = "{sep} ".format(sep=(line_sep).join(it))
103+
out = f"{(line_sep).join(it)} "
104104
return out
105105
else:
106106
it = [
107-
"{key} {val}".format(key=key, val=cls._style_to_xlwt(value, False))
107+
f"{key} {cls._style_to_xlwt(value, False)}"
108108
for key, value in item.items()
109109
]
110-
out = "{sep} ".format(sep=(field_sep).join(it))
110+
out = f"{(field_sep).join(it)} "
111111
return out
112112
else:
113-
item = "{item}".format(item=item)
113+
item = f"{item}"
114114
item = item.replace("True", "on")
115115
item = item.replace("False", "off")
116116
return item

pandas/tests/generic/test_frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def test_set_attribute(self):
196196
def test_to_xarray_index_types(self, index):
197197
from xarray import Dataset
198198

199-
index = getattr(tm, "make{}".format(index))
199+
index = getattr(tm, f"make{index}")
200200
df = DataFrame(
201201
{
202202
"a": list("abc"),

pandas/tests/generic/test_generic.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def test_nonzero(self):
125125
# GH 4633
126126
# look at the boolean/nonzero behavior for objects
127127
obj = self._construct(shape=4)
128-
msg = "The truth value of a {} is ambiguous".format(self._typ.__name__)
128+
msg = f"The truth value of a {self._typ.__name__} is ambiguous"
129129
with pytest.raises(ValueError, match=msg):
130130
bool(obj == 0)
131131
with pytest.raises(ValueError, match=msg):
@@ -203,9 +203,9 @@ def test_constructor_compound_dtypes(self):
203203
def f(dtype):
204204
return self._construct(shape=3, value=1, dtype=dtype)
205205

206-
msg = "compound dtypes are not implemented in the {} constructor".format(
207-
self._typ.__name__
208-
)
206+
msg = "compound dtypes are not implemented"
207+
f"in the {self._typ.__name__} constructor"
208+
209209
with pytest.raises(NotImplementedError, match=msg):
210210
f([("A", "datetime64[h]"), ("B", "str"), ("C", "int32")])
211211

pandas/tests/generic/test_series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def finalize(self, other, method=None, **kwargs):
205205
def test_to_xarray_index_types(self, index):
206206
from xarray import DataArray
207207

208-
index = getattr(tm, "make{}".format(index))
208+
index = getattr(tm, f"make{index}")
209209
s = Series(range(6), index=index(6))
210210
s.index.name = "foo"
211211
result = s.to_xarray()

0 commit comments

Comments
 (0)