From 04a884d7ef6ccfa3f83206aad27c23041f24ad03 Mon Sep 17 00:00:00 2001 From: Linen <29108833+yashukla@users.noreply.github.com> Date: Tue, 12 Nov 2019 15:07:28 -0600 Subject: [PATCH 1/2] CLN: F-string formatting in pandas/tests/indexes/*.py (#29527) --- pandas/tests/indexes/test_base.py | 10 +++++----- pandas/tests/indexes/test_category.py | 4 ++-- pandas/tests/indexes/test_common.py | 7 +++---- pandas/tests/indexes/test_numeric.py | 8 ++++---- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index e43d340a46d9f..800e83841c0e7 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -1837,7 +1837,7 @@ def test_drop_tuple(self, values, to_drop): tm.assert_index_equal(result, expected) removed = index.drop(to_drop[1]) - msg = r"\"\[{}\] not found in axis\"".format(re.escape(to_drop[1].__repr__())) + msg = fr"\"\[{re.escape(to_drop[1].__repr__())}\] not found in axis\"" for drop_me in to_drop[1], [to_drop[1]]: with pytest.raises(KeyError, match=msg): removed.drop(drop_me) @@ -2005,11 +2005,11 @@ def test_isin_level_kwarg_bad_label_raises(self, label, indices): index = indices if isinstance(index, MultiIndex): index = index.rename(["foo", "bar"]) - msg = "'Level {} not found'" + msg = f"'Level {label} not found'" else: index = index.rename("foo") - msg = r"Requested level \({}\) does not match index name \(foo\)" - with pytest.raises(KeyError, match=msg.format(label)): + msg = fr"Requested level \({label}\) does not match index name \(foo\)" + with pytest.raises(KeyError, match=msg): index.isin([], level=label) @pytest.mark.parametrize("empty", [[], Series(), np.array([])]) @@ -2768,7 +2768,7 @@ def test_generated_op_names(opname, indices): # pd.Index.__rsub__ does not exist; though the method does exist # for subclasses. see GH#19723 return - opname = "__{name}__".format(name=opname) + opname = f"__{opname}__" method = getattr(indices, opname) assert method.__name__ == opname diff --git a/pandas/tests/indexes/test_category.py b/pandas/tests/indexes/test_category.py index 61d9d1d70c360..84f98a55376f7 100644 --- a/pandas/tests/indexes/test_category.py +++ b/pandas/tests/indexes/test_category.py @@ -188,8 +188,8 @@ def test_disallow_set_ops(self, func, op_name): # GH 10039 # set ops (+/-) raise TypeError idx = pd.Index(pd.Categorical(["a", "b"])) - msg = "cannot perform {} with this index type: CategoricalIndex" - with pytest.raises(TypeError, match=msg.format(op_name)): + msg = f"cannot perform {op_name} with this index type: CategoricalIndex" + with pytest.raises(TypeError, match=msg): func(idx) def test_method_delegation(self): diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index ae1a21e9b3980..9168cda01e145 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -167,7 +167,7 @@ def test_dtype_str(self, indices): def test_hash_error(self, indices): index = indices with pytest.raises( - TypeError, match=("unhashable type: {0.__name__!r}".format(type(index))) + TypeError, match=(fr"unhashable type: {type(index).__name__!r}") ): hash(indices) @@ -201,9 +201,8 @@ def test_unique(self, indices): with pytest.raises(IndexError, match=msg): indices.unique(level=3) - msg = r"Requested level \(wrong\) does not match index name \({}\)".format( - re.escape(indices.name.__repr__()) - ) + msg = fr"Requested level \(wrong\) does not match index name " \ + fr"\({re.escape(indices.name.__repr__())}\)" with pytest.raises(KeyError, match=msg): indices.unique(level="wrong") diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index e424b3601a4b2..deb63cc9ef854 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -245,9 +245,9 @@ def test_astype(self, mixed_index, float_index): # invalid for dtype in ["M8[ns]", "m8[ns]"]: msg = ( - "Cannot convert Float64Index to dtype {}; integer values" - " are required for conversion" - ).format(pandas_dtype(dtype)) + f"Cannot convert Float64Index to dtype {pandas_dtype(dtype)}; " + f"integer values are required for conversion" + ) with pytest.raises(TypeError, match=re.escape(msg)): i.astype(dtype) @@ -588,7 +588,7 @@ def test_take_fill_value(self): tm.assert_index_equal(result, expected) name = self._holder.__name__ - msg = "Unable to fill values because {name} cannot contain NA".format(name=name) + msg = f"Unable to fill values because {name} cannot contain NA" # fill_value=True with pytest.raises(ValueError, match=msg): From 3aa1ffcfe7c710bda77465374e31153292ad467f Mon Sep 17 00:00:00 2001 From: Linen <29108833+yashukla@users.noreply.github.com> Date: Wed, 13 Nov 2019 16:33:05 -0600 Subject: [PATCH 2/2] Update test_common.py Style changes as per "black pandas". --- pandas/tests/indexes/test_common.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index 9168cda01e145..558ba04b657a1 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -167,7 +167,7 @@ def test_dtype_str(self, indices): def test_hash_error(self, indices): index = indices with pytest.raises( - TypeError, match=(fr"unhashable type: {type(index).__name__!r}") + TypeError, match=(f"unhashable type: {type(index).__name__!r}") ): hash(indices) @@ -201,8 +201,10 @@ def test_unique(self, indices): with pytest.raises(IndexError, match=msg): indices.unique(level=3) - msg = fr"Requested level \(wrong\) does not match index name " \ - fr"\({re.escape(indices.name.__repr__())}\)" + msg = ( + fr"Requested level \(wrong\) does not match index name " + fr"\({re.escape(indices.name.__repr__())}\)" + ) with pytest.raises(KeyError, match=msg): indices.unique(level="wrong")