Skip to content

Commit 94cbc2f

Browse files
Paul LeeTomAugspurger
authored andcommitted
CLN: Update old string formatting to f-string (#30631)
* CLN: Update old string formatting to f-string
1 parent eee7968 commit 94cbc2f

File tree

5 files changed

+17
-24
lines changed

5 files changed

+17
-24
lines changed

pandas/core/ops/array_ops.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,17 +246,16 @@ def comparison_op(
246246
res_values = comp_method_OBJECT_ARRAY(op, lvalues, rvalues)
247247

248248
else:
249-
op_name = "__{op}__".format(op=op.__name__)
249+
op_name = f"__{op.__name__}__"
250250
method = getattr(lvalues, op_name)
251251
with np.errstate(all="ignore"):
252252
res_values = method(rvalues)
253253

254254
if res_values is NotImplemented:
255255
res_values = invalid_comparison(lvalues, rvalues, op)
256256
if is_scalar(res_values):
257-
raise TypeError(
258-
"Could not compare {typ} type with Series".format(typ=type(rvalues))
259-
)
257+
typ = type(rvalues)
258+
raise TypeError(f"Could not compare {typ} type with Series")
260259

261260
return res_values
262261

@@ -293,11 +292,10 @@ def na_logical_op(x: np.ndarray, y, op):
293292
OverflowError,
294293
NotImplementedError,
295294
):
295+
typ = type(y).__name__
296296
raise TypeError(
297-
"Cannot perform '{op}' with a dtyped [{dtype}] array "
298-
"and scalar of type [{typ}]".format(
299-
op=op.__name__, dtype=x.dtype, typ=type(y).__name__
300-
)
297+
f"Cannot perform '{op.__name__}' with a dtyped [{x.dtype}] array "
298+
f"and scalar of type [{typ}]"
301299
)
302300

303301
return result

pandas/core/ops/dispatch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,10 @@ def not_implemented(*args, **kwargs):
210210

211211
if method == "__call__" and op_name in special and kwargs.get("out") is None:
212212
if isinstance(inputs[0], type(self)):
213-
name = "__{}__".format(op_name)
213+
name = f"__{op_name}__"
214214
return getattr(self, name, not_implemented)(inputs[1])
215215
else:
216-
name = flipped.get(op_name, "__r{}__".format(op_name))
216+
name = flipped.get(op_name, f"__r{op_name}__")
217217
return getattr(self, name, not_implemented)(inputs[0])
218218
else:
219219
return NotImplemented

pandas/core/ops/invalid.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,8 @@ def invalid_comparison(left, right, op):
3030
elif op is operator.ne:
3131
res_values = np.ones(left.shape, dtype=bool)
3232
else:
33-
raise TypeError(
34-
"Invalid comparison between dtype={dtype} and {typ}".format(
35-
dtype=left.dtype, typ=type(right).__name__
36-
)
37-
)
33+
typ = type(right).__name__
34+
raise TypeError(f"Invalid comparison between dtype={left.dtype} and {typ}")
3835
return res_values
3936

4037

@@ -52,10 +49,8 @@ def make_invalid_op(name: str):
5249
"""
5350

5451
def invalid_op(self, other=None):
55-
raise TypeError(
56-
"cannot perform {name} with this index type: "
57-
"{typ}".format(name=name, typ=type(self).__name__)
58-
)
52+
typ = type(self).__name__
53+
raise TypeError(f"cannot perform {name} with this index type: {typ}")
5954

6055
invalid_op.__name__ = name
6156
return invalid_op

pandas/core/ops/methods.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ def f(self, other):
102102

103103
return self
104104

105-
f.__name__ = "__i{name}__".format(name=method.__name__.strip("__"))
105+
name = method.__name__.strip("__")
106+
f.__name__ = f"__i{name}__"
106107
return f
107108

108109
new_methods.update(
@@ -214,7 +215,7 @@ def _create_methods(cls, arith_method, comp_method, bool_method, special):
214215
)
215216

216217
if special:
217-
dunderize = lambda x: "__{name}__".format(name=x.strip("_"))
218+
dunderize = lambda x: f"__{x.strip('_')}__"
218219
else:
219220
dunderize = lambda x: x
220221
new_methods = {dunderize(k): v for k, v in new_methods.items()}

pandas/core/ops/roperator.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ def rmod(left, right):
3434
# formatting operation; this is a TypeError
3535
# otherwise perform the op
3636
if isinstance(right, str):
37-
raise TypeError(
38-
"{typ} cannot perform the operation mod".format(typ=type(left).__name__)
39-
)
37+
typ = type(left).__name__
38+
raise TypeError(f"{typ} cannot perform the operation mod")
4039

4140
return right % left
4241

0 commit comments

Comments
 (0)