From 5613129d1de68da19f6534891f4885142dddd995 Mon Sep 17 00:00:00 2001 From: proost Date: Sun, 10 May 2020 02:08:17 +0900 Subject: [PATCH] Replace old string formatting syntax with f-strings (#29547) --- pandas/core/sorting.py | 6 ++---- pandas/tests/indexes/datetimes/test_date_range.py | 4 ++-- pandas/tests/indexes/interval/test_interval.py | 6 +++--- pandas/tests/indexing/test_coercion.py | 3 +-- pandas/tests/reshape/test_concat.py | 11 ++++++----- pandas/util/_decorators.py | 6 +++--- 6 files changed, 17 insertions(+), 19 deletions(-) diff --git a/pandas/core/sorting.py b/pandas/core/sorting.py index 69d55978724af..25312b180dba1 100644 --- a/pandas/core/sorting.py +++ b/pandas/core/sorting.py @@ -406,10 +406,8 @@ def ensure_key_mapped(values, key: Optional[Callable], levels=None): result = type_of_values(result) # try to revert to original type otherwise except TypeError: raise TypeError( - "User-provided `key` function returned an invalid type {} \ - which could not be converted to {}.".format( - type(result), type(values) - ) + f"User-provided `key` function returned an invalid type {type(result)} \ + which could not be converted to {type(values)}." ) return result diff --git a/pandas/tests/indexes/datetimes/test_date_range.py b/pandas/tests/indexes/datetimes/test_date_range.py index d46fe0181ea1b..3d603d64731ee 100644 --- a/pandas/tests/indexes/datetimes/test_date_range.py +++ b/pandas/tests/indexes/datetimes/test_date_range.py @@ -987,8 +987,8 @@ def test_all_custom_freq(self, freq): ) bad_freq = freq + "FOO" - msg = "invalid custom frequency string: {freq}" - with pytest.raises(ValueError, match=msg.format(freq=bad_freq)): + msg = f"invalid custom frequency string: {bad_freq}" + with pytest.raises(ValueError, match=msg): bdate_range(START, END, freq=bad_freq) @pytest.mark.parametrize( diff --git a/pandas/tests/indexes/interval/test_interval.py b/pandas/tests/indexes/interval/test_interval.py index 1b2bfa8573c21..77a1159471661 100644 --- a/pandas/tests/indexes/interval/test_interval.py +++ b/pandas/tests/indexes/interval/test_interval.py @@ -425,10 +425,10 @@ def test_maybe_convert_i8_errors(self, breaks1, breaks2, make_key): key = make_key(breaks2) msg = ( - "Cannot index an IntervalIndex of subtype {dtype1} with " - "values of dtype {dtype2}" + f"Cannot index an IntervalIndex of subtype {breaks1.dtype} with " + f"values of dtype {breaks2.dtype}" ) - msg = re.escape(msg.format(dtype1=breaks1.dtype, dtype2=breaks2.dtype)) + msg = re.escape(msg) with pytest.raises(ValueError, match=msg): index._maybe_convert_i8(key) diff --git a/pandas/tests/indexing/test_coercion.py b/pandas/tests/indexing/test_coercion.py index 5cc2399b6ed72..8c528a521f0ed 100644 --- a/pandas/tests/indexing/test_coercion.py +++ b/pandas/tests/indexing/test_coercion.py @@ -31,8 +31,7 @@ def has_test(combo): for combo in combos: if not has_test(combo): - msg = "test method is not defined: {0}, {1}" - raise AssertionError(msg.format(cls.__name__, combo)) + raise AssertionError(f"test method is not defined: {cls.__name__}, {combo}") yield diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index 04c3ab4285b80..0f13e818e9bda 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -1869,12 +1869,13 @@ def test_concat_invalid(self): # trying to concat a ndframe with a non-ndframe df1 = tm.makeCustomDataframe(10, 2) - msg = ( - "cannot concatenate object of type '{}'; " - "only Series and DataFrame objs are valid" - ) for obj in [1, dict(), [1, 2], (1, 2)]: - with pytest.raises(TypeError, match=msg.format(type(obj))): + + msg = ( + f"cannot concatenate object of type '{type(obj)}'; " + "only Series and DataFrame objs are valid" + ) + with pytest.raises(TypeError, match=msg): concat([df1, obj]) def test_concat_invalid_first_argument(self): diff --git a/pandas/util/_decorators.py b/pandas/util/_decorators.py index b7bdbde5bac5e..13e8dcc067dd4 100644 --- a/pandas/util/_decorators.py +++ b/pandas/util/_decorators.py @@ -289,9 +289,9 @@ def wrapper(*args, **kwargs): num_allow_args = allow_args if len(args) > num_allow_args: msg = ( - "Starting with Pandas version {version} all arguments of {funcname}" - "{except_args} will be keyword-only" - ).format(version=version, funcname=func.__name__, except_args=arguments) + f"Starting with Pandas version {version} all arguments of " + f"{func.__name__}{arguments} will be keyword-only" + ) warnings.warn(msg, FutureWarning, stacklevel=stacklevel) return func(*args, **kwargs)