Skip to content

Commit a626d47

Browse files
authored
Replace old string formatting syntax with f-strings (#29547) (#34094)
1 parent 648cd71 commit a626d47

File tree

6 files changed

+17
-19
lines changed

6 files changed

+17
-19
lines changed

pandas/core/sorting.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -406,10 +406,8 @@ def ensure_key_mapped(values, key: Optional[Callable], levels=None):
406406
result = type_of_values(result) # try to revert to original type otherwise
407407
except TypeError:
408408
raise TypeError(
409-
"User-provided `key` function returned an invalid type {} \
410-
which could not be converted to {}.".format(
411-
type(result), type(values)
412-
)
409+
f"User-provided `key` function returned an invalid type {type(result)} \
410+
which could not be converted to {type(values)}."
413411
)
414412

415413
return result

pandas/tests/indexes/datetimes/test_date_range.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -987,8 +987,8 @@ def test_all_custom_freq(self, freq):
987987
)
988988

989989
bad_freq = freq + "FOO"
990-
msg = "invalid custom frequency string: {freq}"
991-
with pytest.raises(ValueError, match=msg.format(freq=bad_freq)):
990+
msg = f"invalid custom frequency string: {bad_freq}"
991+
with pytest.raises(ValueError, match=msg):
992992
bdate_range(START, END, freq=bad_freq)
993993

994994
@pytest.mark.parametrize(

pandas/tests/indexes/interval/test_interval.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,10 @@ def test_maybe_convert_i8_errors(self, breaks1, breaks2, make_key):
425425
key = make_key(breaks2)
426426

427427
msg = (
428-
"Cannot index an IntervalIndex of subtype {dtype1} with "
429-
"values of dtype {dtype2}"
428+
f"Cannot index an IntervalIndex of subtype {breaks1.dtype} with "
429+
f"values of dtype {breaks2.dtype}"
430430
)
431-
msg = re.escape(msg.format(dtype1=breaks1.dtype, dtype2=breaks2.dtype))
431+
msg = re.escape(msg)
432432
with pytest.raises(ValueError, match=msg):
433433
index._maybe_convert_i8(key)
434434

pandas/tests/indexing/test_coercion.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ def has_test(combo):
3131

3232
for combo in combos:
3333
if not has_test(combo):
34-
msg = "test method is not defined: {0}, {1}"
35-
raise AssertionError(msg.format(cls.__name__, combo))
34+
raise AssertionError(f"test method is not defined: {cls.__name__}, {combo}")
3635

3736
yield
3837

pandas/tests/reshape/test_concat.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1869,12 +1869,13 @@ def test_concat_invalid(self):
18691869

18701870
# trying to concat a ndframe with a non-ndframe
18711871
df1 = tm.makeCustomDataframe(10, 2)
1872-
msg = (
1873-
"cannot concatenate object of type '{}'; "
1874-
"only Series and DataFrame objs are valid"
1875-
)
18761872
for obj in [1, dict(), [1, 2], (1, 2)]:
1877-
with pytest.raises(TypeError, match=msg.format(type(obj))):
1873+
1874+
msg = (
1875+
f"cannot concatenate object of type '{type(obj)}'; "
1876+
"only Series and DataFrame objs are valid"
1877+
)
1878+
with pytest.raises(TypeError, match=msg):
18781879
concat([df1, obj])
18791880

18801881
def test_concat_invalid_first_argument(self):

pandas/util/_decorators.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ def wrapper(*args, **kwargs):
289289
num_allow_args = allow_args
290290
if len(args) > num_allow_args:
291291
msg = (
292-
"Starting with Pandas version {version} all arguments of {funcname}"
293-
"{except_args} will be keyword-only"
294-
).format(version=version, funcname=func.__name__, except_args=arguments)
292+
f"Starting with Pandas version {version} all arguments of "
293+
f"{func.__name__}{arguments} will be keyword-only"
294+
)
295295
warnings.warn(msg, FutureWarning, stacklevel=stacklevel)
296296
return func(*args, **kwargs)
297297

0 commit comments

Comments
 (0)