Skip to content

CLN: Use fstring instead of .format in io/excel and test/generic #30601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions pandas/io/excel/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,7 @@ def read_excel(

for arg in ("sheet", "sheetname", "parse_cols"):
if arg in kwds:
raise TypeError(
"read_excel() got an unexpected keyword argument `{}`".format(arg)
)
raise TypeError(f"read_excel() got an unexpected keyword argument `{arg}`")

if not isinstance(io, ExcelFile):
io = ExcelFile(io, engine=engine)
Expand Down Expand Up @@ -429,7 +427,7 @@ def parse(

for asheetname in sheets:
if verbose:
print("Reading sheet {sheet}".format(sheet=asheetname))
print(f"Reading sheet {asheetname}")

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

try:
engine = config.get_option("io.excel.{ext}.writer".format(ext=ext))
engine = config.get_option(f"io.excel.{ext}.writer")
if engine == "auto":
engine = _get_default_writer(ext)
except KeyError:
raise ValueError("No engine for filetype: '{ext}'".format(ext=ext))
raise ValueError(f"No engine for filetype: '{ext}'")
cls = get_writer(engine)

return object.__new__(cls)
Expand Down Expand Up @@ -757,9 +755,8 @@ def check_extension(cls, ext):
if ext.startswith("."):
ext = ext[1:]
if not any(ext in extension for extension in cls.supported_extensions):
msg = "Invalid extension for engine '{engine}': '{ext}'".format(
engine=pprint_thing(cls.engine), ext=pprint_thing(ext)
)
msg = "Invalid extension for engine"
f"'{pprint_thing(cls.engine)}': '{pprint_thing(ext)}'"
raise ValueError(msg)
else:
return True
Expand Down Expand Up @@ -802,7 +799,7 @@ def __init__(self, io, engine=None):
if engine is None:
engine = "xlrd"
if engine not in self._engines:
raise ValueError("Unknown engine: {engine}".format(engine=engine))
raise ValueError(f"Unknown engine: {engine}")

self.engine = engine
# could be a str, ExcelFile, Book, etc.
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/excel/_odfreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,4 @@ def _get_cell_value(self, cell, convert_float: bool) -> Scalar:
elif cell_type == "time":
return pd.to_datetime(str(cell)).time()
else:
raise ValueError("Unrecognized type {}".format(cell_type))
raise ValueError(f"Unrecognized type {cell_type}")
2 changes: 1 addition & 1 deletion pandas/io/excel/_openpyxl.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def _convert_to_style_kwargs(cls, style_dict):
for k, v in style_dict.items():
if k in _style_key_map:
k = _style_key_map[k]
_conv_to_x = getattr(cls, "_convert_to_{k}".format(k=k), lambda x: None)
_conv_to_x = getattr(cls, f"_convert_to_{k}", lambda x: None)
new_v = _conv_to_x(v)
if new_v:
style_kwargs[k] = new_v
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/excel/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def get_writer(engine_name):
try:
return _writers[engine_name]
except KeyError:
raise ValueError("No Excel writer '{engine}'".format(engine=engine_name))
raise ValueError(f"No Excel writer '{engine_name}'")


def _excel2num(x):
Expand Down Expand Up @@ -76,7 +76,7 @@ def _excel2num(x):
cp = ord(c)

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

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

Expand Down
10 changes: 5 additions & 5 deletions pandas/io/excel/_xlwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,20 @@ def _style_to_xlwt(
if hasattr(item, "items"):
if firstlevel:
it = [
"{key}: {val}".format(key=key, val=cls._style_to_xlwt(value, False))
f"{key}: {cls._style_to_xlwt(value, False)}"
for key, value in item.items()
]
out = "{sep} ".format(sep=(line_sep).join(it))
out = f"{(line_sep).join(it)} "
return out
else:
it = [
"{key} {val}".format(key=key, val=cls._style_to_xlwt(value, False))
f"{key} {cls._style_to_xlwt(value, False)}"
for key, value in item.items()
]
out = "{sep} ".format(sep=(field_sep).join(it))
out = f"{(field_sep).join(it)} "
return out
else:
item = "{item}".format(item=item)
item = f"{item}"
item = item.replace("True", "on")
item = item.replace("False", "off")
return item
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/generic/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def test_set_attribute(self):
def test_to_xarray_index_types(self, index):
from xarray import Dataset

index = getattr(tm, "make{}".format(index))
index = getattr(tm, f"make{index}")
df = DataFrame(
{
"a": list("abc"),
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/generic/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def test_nonzero(self):
# GH 4633
# look at the boolean/nonzero behavior for objects
obj = self._construct(shape=4)
msg = "The truth value of a {} is ambiguous".format(self._typ.__name__)
msg = f"The truth value of a {self._typ.__name__} is ambiguous"
with pytest.raises(ValueError, match=msg):
bool(obj == 0)
with pytest.raises(ValueError, match=msg):
Expand Down Expand Up @@ -203,9 +203,9 @@ def test_constructor_compound_dtypes(self):
def f(dtype):
return self._construct(shape=3, value=1, dtype=dtype)

msg = "compound dtypes are not implemented in the {} constructor".format(
self._typ.__name__
)
msg = "compound dtypes are not implemented"
f"in the {self._typ.__name__} constructor"

with pytest.raises(NotImplementedError, match=msg):
f([("A", "datetime64[h]"), ("B", "str"), ("C", "int32")])

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/generic/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def finalize(self, other, method=None, **kwargs):
def test_to_xarray_index_types(self, index):
from xarray import DataArray

index = getattr(tm, "make{}".format(index))
index = getattr(tm, f"make{index}")
s = Series(range(6), index=index(6))
s.index.name = "foo"
result = s.to_xarray()
Expand Down