Skip to content

CLN: GH29547 format with f-strings #34502

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 8 commits into from
Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 9 additions & 7 deletions pandas/tests/series/indexing/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,19 @@ def test_setitem_float_labels():


def test_slice_float_get_set(datetime_series):
msg = (
"cannot do slice indexing on DatetimeIndex with these indexers "
r"\[{key}\] of type float"
msg = lambda key: (
f"cannot do slice indexing on DatetimeIndex with these indexers "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the f here and run black to get linting green? I would wait for one of the core devs to give an opinion but usually def is preferred to a named lambda.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for your response.

if i remove the f in the f-string the code doesn't compile.
may i ask why def is preferred for this? i think that lambda suits better because it's a short statement.
also, i published my solution in the thread but didn't get any response

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using def vs. lambda is partly a stylistic thing, with the idea being that lambdas are intended as anonymous functions, so naming them somewhat misses the point.

Did you remove the f from all strings? You only need to remove it from the strings for which it has no effect, i.e., when there is nothing to evaluate (e.g., line 87).

f"\[{key}\] of type float"
)
with pytest.raises(TypeError, match=msg.format(key=r"4\.0")):
with pytest.raises(TypeError, match=msg(key=r"4\.0")):
datetime_series[4.0:10.0]

with pytest.raises(TypeError, match=msg.format(key=r"4\.0")):
with pytest.raises(TypeError, match=msg(key=r"4\.0")):
datetime_series[4.0:10.0] = 0

with pytest.raises(TypeError, match=msg.format(key=r"4\.5")):
with pytest.raises(TypeError, match=msg(key=r"4\.5")):
datetime_series[4.5:10.0]
with pytest.raises(TypeError, match=msg.format(key=r"4\.5")):
with pytest.raises(TypeError, match=msg(key=r"4\.5")):
datetime_series[4.5:10.0] = 0


Expand Down Expand Up @@ -134,3 +134,5 @@ def test_int_indexing():
def test_getitem_int64(datetime_series):
idx = np.int64(5)
assert datetime_series[idx] == datetime_series[5]


9 changes: 6 additions & 3 deletions pandas/tests/series/indexing/test_take.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ def test_take():
expected = Series([4, 2, 4], index=[4, 3, 4])
tm.assert_series_equal(actual, expected)

msg = "index {} is out of bounds for( axis 0 with)? size 5"
with pytest.raises(IndexError, match=msg.format(10)):
msg = lambda x: f"index {x} is out of bounds for( axis 0 with)? size 5"
with pytest.raises(IndexError, match=msg(10)):
ser.take([1, 10])
with pytest.raises(IndexError, match=msg.format(5)):
with pytest.raises(IndexError, match=msg(5)):
ser.take([2, 5])


Expand All @@ -31,3 +31,6 @@ def test_take_categorical():
pd.Categorical(["b", "b", "a"], categories=["a", "b", "c"]), index=[1, 1, 0]
)
tm.assert_series_equal(result, expected)



14 changes: 7 additions & 7 deletions pandas/tests/series/indexing/test_where.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,11 @@ def test_where_setitem_invalid():
# GH 2702
# make sure correct exceptions are raised on invalid list assignment

msg = "cannot set using a {} indexer with a different length than the value"

msg = lambda x : f"cannot set using a {x} indexer with a different length than the value"
# slice
s = Series(list("abc"))

with pytest.raises(ValueError, match=msg.format("slice")):
with pytest.raises(ValueError, match=msg("slice")):
s[0:3] = list(range(27))

s[0:3] = list(range(3))
Expand All @@ -237,7 +236,7 @@ def test_where_setitem_invalid():
# slice with step
s = Series(list("abcdef"))

with pytest.raises(ValueError, match=msg.format("slice")):
with pytest.raises(ValueError, match=msg("slice")):
s[0:4:2] = list(range(27))

s = Series(list("abcdef"))
Expand All @@ -248,7 +247,7 @@ def test_where_setitem_invalid():
# neg slices
s = Series(list("abcdef"))

with pytest.raises(ValueError, match=msg.format("slice")):
with pytest.raises(ValueError, match=msg("slice")):
s[:-1] = list(range(27))

s[-3:-1] = list(range(2))
Expand All @@ -258,12 +257,12 @@ def test_where_setitem_invalid():
# list
s = Series(list("abc"))

with pytest.raises(ValueError, match=msg.format("list-like")):
with pytest.raises(ValueError, match=msg("list-like")):
s[[0, 1, 2]] = list(range(27))

s = Series(list("abc"))

with pytest.raises(ValueError, match=msg.format("list-like")):
with pytest.raises(ValueError, match=msg("list-like")):
s[[0, 1, 2]] = list(range(2))

# scalar
Expand Down Expand Up @@ -443,3 +442,4 @@ def test_where_sparse():
result = ser.where(ser >= 2, 0)
expected = pd.Series(pd.arrays.SparseArray([0, 2]))
tm.assert_series_equal(result, expected)